我试图合并两个xml文件,但面临一些问题

时间:2015-10-20 08:39:05

标签: c# xml

Xml文件1 -

<?xml version="1.0" encoding="utf-8"?>
<Employees>
  <Employee Id="1">    
    <FirstName>Aa</FirstName>
    <LastName>PDC1</LastName>
    <Salary>
      <subsal>1</subsal>
    </Salary>
  </Employee>
 <Employee Id="2">    
    <FirstName>Bb</FirstName>
    <LastName>PDC2A</LastName>
    <Salary>
      <subsal>2</subsal>
    </Salary>
  </Employee>
  <Employee Id="3">    
    <FirstName>Cc</FirstName>
    <LastName>PDC2B</LastName>
    <Salary>
      <subsal>3</subsal>
    </Salary>
  </Employee>
</Employees>

XML文件2 - :

<?xml version="1.0" encoding="utf-8"?>
<Employees>
  <Employee Id="1">    
    <FirstName>AaA</FirstName>
    <LastName>PDC1</LastName>
    <Salary>
      <subsal>4</subsal>
    </Salary>    
  </Employee>
 <Employee Id="2">    
    <FirstName>BbB</FirstName>
    <LastName>PDC2A</LastName>
    <Salary>
      <subsal>5</subsal>
    </Salary>
  </Employee>
 </Employees>

我必须将这些文件合并为一个,并使用以下代码 - :

using System;
using System.Xml;
using System.IO;
using System.Data;

namespace merge_xml
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                XmlTextReader xmlreader1 = new XmlTextReader("C:\\Users\\saket.parasar.jha\\s.xml");
                XmlTextReader xmlreader2 = new XmlTextReader("C:\\Users\\saket.parasar.jha\\s1.xml");

                DataSet ds = new DataSet();
                ds.ReadXml(xmlreader1);

                DataSet ds2 = new DataSet();
                ds2.ReadXml(xmlreader2);

                ds.Merge(ds2);

                Console.WriteLine("Completed merging XML documents");

                ds.WriteXml("C:\\Users\\saket.parasar.jha\\sneww.xml");

                ////XmlDocument doc = new XmlDocument();
                ////doc.Load(("C:\\Users\\saket.parasar.jha\\sneww.xml"));
                ////XmlNodeList nodes = doc.SelectNodes("//product/id");
                //////int nNodeID = nodes.Count;
                ////nNodeID++;
            }

            catch (System.Exception ex)
            {
                Console.Write(ex.Message);
            }
            Console.Read(); 
        }
    }
}

我得到的输出是 - :

<?xml version="1.0" standalone="yes"?>
<Employees>
  <Employee Id="1">
    <FirstName>AaA</FirstName>
    <LastName>PDC1</LastName>
    <Salary>
      <subsal>1</subsal>
    </Salary>
    <Salary>
      <subsal>4</subsal>
    </Salary>
  </Employee>
  <Employee Id="2">
    <FirstName>BbB</FirstName>
    <LastName>PDC2A</LastName>
    <Salary>
      <subsal>2</subsal>
    </Salary>
    <Salary>
      <subsal>5</subsal>
    </Salary>
  </Employee>
  <Employee Id="3">
    <FirstName>Cc</FirstName>
    <LastName>PDC2B</LastName>
    <Salary>
      <subsal>3</subsal>
    </Salary>
  </Employee>
</Employees>

所需的输出也应该是 - :

<?xml version="1.0" encoding="utf-8"?>
<Employees>
  <Employee Id="1">    
    <FirstName>Aa</FirstName>
    <LastName>PDC1</LastName>
    <Salary>
      <subsal>1</subsal>
    </Salary>
  </Employee>
 <Employee Id="2">    
    <FirstName>Bb</FirstName>
    <LastName>PDC2A</LastName>
    <Salary>
      <subsal>2</subsal>
    </Salary>
  </Employee>
  <Employee Id="3">    
    <FirstName>Cc</FirstName>
    <LastName>PDC2B</LastName>
    <Salary>
      <subsal>3</subsal>
    </Salary>
  </Employee>

<Employees>
  <Employee Id="4">    
    <FirstName>AaA</FirstName>
    <LastName>PDC1</LastName>
    <Salary>
      <subsal>4</subsal>
    </Salary>    
  </Employee>
 <Employee Id="5">    
    <FirstName>BbB</FirstName>
    <LastName>PDC2A</LastName>
    <Salary>
      <subsal>6</subsal>
    </Salary>
  </Employee>
 </Employees>

</Employees>

我应该尝试一些其他的合并方法,如果可能的话请建议一些代码..

1 个答案:

答案 0 :(得分:0)

像这样修改XML file 2-:文件:

<?xml version="1.0" encoding="utf-8"?>
<Employees>
   <Employee Id="4">    
       <FirstName>AaA</FirstName>
       <LastName>PDC1</LastName>
       <Salary>
          <subsal>4</subsal>
       </Salary>    
   </Employee>
   <Employee Id="5">    
       <FirstName>BbB</FirstName>
       <LastName>PDC2A</LastName>
       <Salary>
          <subsal>5</subsal>
       </Salary>
   </Employee>
</Employees>

更改Employee Id,您将得到您想要的内容。