重复表的复制操作

using Microsoft.Office.InfoPath;
using System;
using System.Xml;
using System.Xml.XPath;

namespace copytable
{
    public partial class FormCode
    {
        // 注意: 以下是 Microsoft Office InfoPath 所需的过程。
        // 可以使用 Microsoft Office InfoPath 对其进行修改。
        public void InternalStartup()
        {
            EventManager.XmlEvents["/my:myFields/my:group1/my:group2"].Changed += new XmlChangedEventHandler(group2_Changed);
        }

        public void group2_Changed(object sender, XmlEventArgs e)
        {
            DelRange("/my:myFields/my:group3", "my:group4");

            string strExpression = @"/my:myFields/my:group1/my:group2[my:check1='Y']";
            string toNav = "/my:myFields/my:group3/my:group4[last()]";
            string[] fromNodes ={ "my:field2", "my:field3" };
            string[] toNodes ={ "my:field4", "my:field5" };
            copyTable(strExpression, toNav, fromNodes, toNodes);

            AddBlank("/my:myFields/my:group3/my:group4[last()]");

        }

        public void copyTable(string strExpression, string toNav, string[] fromNodes, string[] toNodes)
        {
            XPathNavigator xn = MainDataSource.CreateNavigator();
            XPathNodeIterator itor = xn.Select(strExpression, NamespaceManager);
            XPathNavigator to = this.MainDataSource.CreateNavigator().SelectSingleNode(toNav, this.NamespaceManager);
            XPathNavigator clone = null;

            while (itor.MoveNext())
            {
                {
                    clone = to.Clone();
                    for (int i = 0; i < fromNodes.Length;i++ )
                    {
                        to.SelectSingleNode(toNodes[i], NamespaceManager).SetValue(itor.Current.SelectSingleNode(fromNodes[i], NamespaceManager).Value);
                    }
                    to.InsertBefore(clone);
                }

            }
        }

        public void DelRange(string body, string node)
        {
            try
            {
                XPathNavigator parent = this.MainDataSource.CreateNavigator().SelectSingleNode(body, this.NamespaceManager);

                if (parent != null)
                {

                    XPathNodeIterator rowitor = parent.Select(node, this.NamespaceManager);
                    XPathNavigator xn = this.MainDataSource.CreateNavigator();

                    XPathNavigator n1 = xn.SelectSingleNode(body + "/" + node + "[1]", this.NamespaceManager);
                    XPathNavigator n2 = n1.SelectSingleNode(body + "/" + node + "[" + (rowitor.Count - 1) + "]", this.NamespaceManager);
                    xn.MoveTo(n1);

                    if (rowitor.Count > 1)
                    {
                        xn.DeleteRange(n2);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public void DeleteAll(string body,string node)
        {

            XPathNavigator parent = this.MainDataSource.CreateNavigator().SelectSingleNode(body, this.NamespaceManager);

            if (parent != null)
            {

                XPathNodeIterator rowitor = parent.Select(node, this.NamespaceManager);
                if (rowitor.Count > 0)
                {
                    for (int i = 0; i < Convert.ToInt32(rowitor.Count)-1; i++)
                    {
                        rowitor.Current.DeleteSelf();
                        rowitor.MoveNext();

                    }
                }
            }
 

        }

        public void AddBlank(string node)
        {
            try
            {
                XPathNavigator group = this.MainDataSource.CreateNavigator().SelectSingleNode(node, this.NamespaceManager);
                group.SelectSingleNode("my:field4", NamespaceManager).SetValue("");
                group.SelectSingleNode("my:field5", NamespaceManager).SetValue("");
                group.SelectSingleNode("my:field6", NamespaceManager).SetValue("");

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
    }
}

posted @ 2009-03-04 16:21  昕友软件开发  阅读(284)  评论(0编辑  收藏  举报
欢迎访问我的开源项目:xyIM企业即时通讯