tags:

views:

74

answers:

4

Hi all,

I need a c# lang code to merge two xml files into one, from the specified content.

XML FILE 1 :

 <exchange-documents>
    <documentlegal>
      <bibliographic-data>        
          <applicants>
              <applicant-name>
                <name>CENTURY PRODUCTS CO [US]</name>
              </applicant-name>                         
          </applicants>       
       </bibliographic-data>
    </documentlegal>
  </exchange-documents>

XML FILE 2:

<exchange-documents>
    <documentpatent>
      <bibliographic-data>        
          <applicants>
              <applicant-name>
                <name>CENTURY PRODUCTS CO [US]</name>
              </applicant-name>                         
          </applicants>       
       </bibliographic-data>
    </documentpatent>
  </exchange-documents>

I need to read the above two xml files and write it into a new xml files with selected elements?

OUTPUT XML:

<documentlegal>
          <bibliographic-data>        
              <applicants>
                  <applicant-name>
                    <name>CENTURY PRODUCTS CO [US]</name>
                  </applicant-name>                         
              </applicants>       
           </bibliographic-data>
        </documentlegal>
  <documentpatent>
          <bibliographic-data>        
              <applicants>
                  <applicant-name>
                    <name>CENTURY PRODUCTS CO [US]</name>
                  </applicant-name>                         
              </applicants>       
           </bibliographic-data>
        </documentpatent>

I dont need the exchnage document element. Can anyone provide me a c# code to achiev the above scenario?

A: 

Read Merging XML Files, Schema Validation, and More on MSDN.

ARS
A: 

You may want to look into using linq to xml. It is a very easy way to work with xml data. Here is a link to a site that does some examples, but others can easily be found by way of google.

czuroski
A: 

This is my code. I am able get it for one xml file. How to merget he other one.

 XmlDocument doc = new XmlDocument();
                doc.Load(@"e://file_2.xml");                
                XmlNodeList xlist = doc.GetElementsByTagName("documentlegal");
                int j=xlist.Count;
                for (int i = 0; i <= j; i++)
                {
                Console.WriteLine(xlist.Item(i).InnerXml);                
                }
                Console.ReadLine();
                }
Googler
A: 

I would create class(es) that represent the XML file format(s) that have the ability to serialize and deserialize both objects. I would then use code to deserialize the objects and merge them. (maybe not the most efficient way, but item means you know what you're dealing with and will enable you to isolate/find/resolve issues quicker)

Mark Redman
...sorry realised you wanted code, you will need to investigate the actual workings yourself...
Mark Redman