我想做到的就是把XML Doc1 和 XML Doc2 合成为 XML Doc3 的样子,大概的效果就是把前几层结构相同的子项合并到一起.研究一晚上,,没找到简单的办法,请各种牛指教XML Documnet 1<?xml version="1.0" encoding="utf-8"?>
<root>
  <typekind text="食品">
    <typekindthinglist>
      <thing_id id="dd59927" title="月饼" url=""></thing_id>
      <thing_id id="59dddd7" title="豆沙包" url=""></thing_id>
    </typekindthinglist>
    <typekindchildtype>
             <typekind text="小菜">
             <typekindthinglist>
                  <thing_id id="dd59927" title="花生米" url=""></thing_id>
                  <thing_id id="59dddd7" title="蚕豆" url=""></thing_id>
             </typekindthinglist>
             <typekindchildtype>
             </typekindchildtype>
    </typekindchildtype>
  </typekind>
 
</root>XML Document 2<?xml version="1.0" encoding="utf-8"?>
<root>
  <typekind text="食品">
    <typekindthinglist>
      <thing_id id="dd59927" title="月饼" url="">
      </thing_id>
      <thing_id id="59dddd7" title="豆沙包" url="">
      </thing_id>
    </typekindthinglist>
    <typekindchildtype>             <typekind text="主食">
             <typekindthinglist>
                  <thing_id id="dd59927" title="米饭" url=""></thing_id>
                  <thing_id id="59dddd7" title="面条" url=""></thing_id>
             </typekindthinglist>
             <typekindchildtype>
             </typekindchildtype>
    </typekindchildtype>
  </typekind>
</root>
XML Document 3<?xml version="1.0" encoding="utf-8"?>
<root>
  <typekind text="食品">
    <typekindthinglist>
      <thing_id id="dd59927" title="月饼" url=""></thing_id>
      <thing_id id="59dddd7" title="豆沙包" url=""></thing_id>
    </typekindthinglist>
    <typekindchildtype>
             <typekind text="小菜">
             <typekindthinglist>
                  <thing_id id="dd59927" title="花生米" url=""></thing_id>
                  <thing_id id="59dddd7" title="蚕豆" url=""></thing_id>
             </typekindthinglist>
             <typekindchildtype>
             </typekindchildtype>
             <typekind text="主食">
             <typekindthinglist>
                  <thing_id id="dd59927" title="米饭" url=""></thing_id>
                  <thing_id id="59dddd7" title="面条" url=""></thing_id>
             </typekindthinglist>
             <typekindchildtype>
             </typekindchildtype>
    </typekindchildtype>
  </typekind>
 
</root>

解决方案 »

  1.   

    分别读进2个DataSet,然后合并它们(DataSet有合并方法,不记得哪个了),再输出。
      

  2.   

    读成俩个DataTable然后用Union把俩表连合去除重复项(不加all)合成一个表再读成XML试试-----------------------------------------------
    下面的查询在这两个表之间创建 UNION 运算:SELECT * FROM Table1
    UNION
    SELECT * FROM Table2下面是结果集:ColumnA  ColumnB
    -------  --------
    abc      1
    def      2
    ghi      3
    jkl      4
    mno      5
      

  3.   

    在.net下
    用如下方法生成dataset
    public static DataSet Run(String[] args)
    {
    DataSet ds=new DataSet();
    try
    {
    XmlDataDocument myXmlDataDocument = new XmlDataDocument();
    ParseSchema(args[1],myXmlDataDocument);
    myXmlDataDocument.Load(args[0]);
    ds=myXmlDataDocument.DataSet;
    return ds;
    }
    catch (Exception e)
    {
    throw new Exception(e.Message);
    }

    } // 向数据集中加载指定架构
    public static void ParseSchema(String schema,XmlDataDocument xmldoc)
    {
    StreamReader myStreamReader = null;
    try
    {
    myStreamReader = new StreamReader(schema);     xmldoc.DataSet.ReadXmlSchema(myStreamReader);
    } catch (Exception e)
    {
    throw new Exception(e.Message);
    } finally
    {
    if (myStreamReader != null)
    myStreamReader.Close();
    }
    }
    ////////////////////////////////////////////
    分别调用ds然后循环生成xml
      

  4.   

    实际上用xsl也是可以的.
    方式是使用<xsl:element>
      

  5.   

    要用dataset合并的话,不能写成<thing_id id="dd59927" title="月饼" url=""></thing_id>
    最好用 <thing_id><id></id><thing_id>
    ds.暂时不支持3层以上的嵌套格式,建议用 LoadXml()单独读出表来
      

  6.   


    读取数据集里面,利用DataSet现成的Merge功能!
      

  7.   

    首先你的两个xml文档都不是良构的,麻烦改正确
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml;namespace ConsoleApplication1
    {
        class Program
        {
            private static XmlDocument xd3;
            static void Main(string[] args)
            {
                try
                {
                    //读取第一个xml文档
                    XmlDocument xd1 = new XmlDocument();
                    xd1.Load("../../xmlfile1.xml");
                    XmlElement xe1 = xd1.DocumentElement;
                    //读取第二个xml文档
                    XmlDocument xd2 = new XmlDocument();
                    xd2.Load("../../xmlfile2.xml");
                    XmlElement xe2 = xd2.DocumentElement;
                    //构建新的文档
                    xd3 = new XmlDocument();
                    xd3.LoadXml ("<root></root>");
                    XmlElement xe3 = xd3.DocumentElement;
                    xd3.InsertBefore(xd3.CreateXmlDeclaration("1.0","utf-8",null),xe3);
                    CloneXmlNode(xe1, xe3);
                    CloneXmlNode(xe2, xe3);
                    xd3.Save("../../xmlfile3.xml");
                    
                    
                }
                catch (Exception excep)
                {
                    Console.Write(excep.ToString());
                }
                finally {
                    Console.Read();
                }
            }        static void CloneXmlNode(XmlNode xnsour, XmlNode xndesc) {
                //分类typekind列表
                XmlNodeList xnl = xnsour.SelectNodes("typekind");
                foreach (XmlNode xn in xnl) {
                    string text = xn.Attributes["text"].Value;
                    XmlNode xnt=xndesc.SelectSingleNode(String.Format("typekind[@text=\"{0}\"]", text));
                    if ( xnt == null) {
                        //如果找不到分类,创建分类
                        xnt = xd3.CreateElement("typekind");
                        XmlAttribute xatext=xd3.CreateAttribute("text");
                        xatext.Value = text;
                        xnt.Attributes.Append(xatext);
                        xndesc.AppendChild(xnt);
                    }
                    //叶列表
                    //typekindthinglist
                    XmlNode typekindthinglistSour = xn.SelectSingleNode("typekindthinglist");
                    XmlNode typekindthinglistDesc = xnt.SelectSingleNode("typekindthinglist");
                    if (typekindthinglistDesc == null) {
                        typekindthinglistDesc = xd3.CreateElement("typekindthinglist");
                        xnt.AppendChild(typekindthinglistDesc);
                    }
                    //读取叶节点
                    XmlNodeList thing_idSourS= typekindthinglistSour.SelectNodes("thing_id");
                    foreach (XmlNode thing_idSour in thing_idSourS) {
                        string id = thing_idSour.Attributes["id"].Value;
                        XmlNode thing_idDesc = typekindthinglistDesc.SelectSingleNode(String.Format("thing_id[@id=\"{0}\"]", id));
                        if (thing_idDesc == null)
                        { 
                            //如果目标中不存在这样的叶节点,加入之
                            thing_idDesc = xd3.CreateElement("thing_id");
                            for (int i = 0; i < thing_idSour.Attributes.Count; i++) {
                                XmlAttribute xatemp = xd3.CreateAttribute(thing_idSour.Attributes[i].Name);
                                xatemp.Value = thing_idSour.Attributes[i].Value;
                                thing_idDesc.Attributes.Append(xatemp);
                            }
                            typekindthinglistDesc.AppendChild(thing_idDesc);
                        }
                    }
                    //子节点集合
                    //typekindchildtype
                    XmlNode typekindchildtypeSour = xn.SelectSingleNode("typekindchildtype");
                    XmlNode typekindchildtypeDesc = xnt.SelectSingleNode("typekindchildtype");
                    if (typekindchildtypeDesc == null)
                    {
                        typekindchildtypeDesc = xd3.CreateElement("typekindchildtype");
                        xnt.AppendChild(typekindchildtypeDesc);
                    }
                   
                    if (typekindchildtypeSour != null) {
                       
                        CloneXmlNode(typekindchildtypeSour, typekindchildtypeDesc);
                    }            }
               
            }
        }
    }
      

  8.   

    观察每个分类节点
    都具有相似的结构
     <typekind text="主食">
            <typekindthinglist>
              <thing_id id="dd59927" title="米饭" url=""></thing_id>
              <thing_id id="59dddd7" title="面条" url=""></thing_id>
            </typekindthinglist>
            <typekindchildtype>
            </typekindchildtype>
          </typekind>
    可简化为<typekind><typekindthinglist/><typekindchildtype/></typekind>
    解决思路就是先找到每层的typekind列表
    如果目标文档中存在路径相同text属性相同的typekind节点,则直接在此节点添加typekindthinglist和typekindchildtype节点集合,反之创建
    对于typekindthinglist的节点本身亦然
    如果typekingthinglist的thing_id集合中已有id属性相同的子节点,则不做任何处理(可能xmlfile1,xmlfile2中有相同路径相同id属性的thing_id,不认为是两种商品),反之添加thing_id节点
    对于typekindchildtype结点下的每一个子节点,都是一个typekind
    则递归重复以上过程