0

I want to copy (element/attribute)values from one XML file to another but at the moment I cannot figure out how to go about it. I have values in file A which I want to copy to file B. File B has same elements/attributes more or less with the only difference of being empty. I am taking this approach since I don't have the schema for neither file.

Following are the contents of file A:

<status>1</status>
  <arguments>
  <argument name="ZONE">
     <value>ZONE 1</value>
  </argument>      
  <argument name="JOB_DATES">
     <argument name="JOB_DATE">
        <value>2014-01-20</value>
     </argument>
  </argument>
  <argument name="PERSON">
     <argument name="NAME_1">
        <value>JOHN</value>
     </argument>
     <argument name="NAME_2">
        <value>SMITH</value>
     </argument>
  </argument>      
  <argument name="FIRST_SCHEDULE_JOB">
     <value>true</value>
  </argument>      
  <argument name="EMPLOYEE">
     <value>ABXX011</value>
  </argument>
</arguments>
<place placeType="JOB_SITE">
  <site>
     <street>DUKE 2</street>
     <house_name>TECH HOUSE</house_name>
     <zip>QZ12324</zip>
     <city>NYC</city>
     <province>NY</province>
     <country>USA</country>
  </site>
  <contact>
     <Name>JOHN</Name>
     <Name_1>SMITH</Name_1>
     <address>
        <street>DUKE 2</street>
        <house_name>TECH HOUSE</house_name>
        <zip>QZ12324</zip>
        <city>NYC</city>
        <province>NY</province>
        <country>USA</country>
     </address>
  </contact>      

Following are the contents of file B:

<status></status>
<arguments>
  <argument name="ZONE">
     <value></value>
  </argument>      
  <argument name="JOB_DATES">
     <argument name="JOB_DATE">
        <value></value>
     </argument>
  </argument>
  <argument name="PERSON">
     <argument name="NAME_1">
        <value></value>
     </argument>
     <argument name="NAME_2">
        <value></value>
     </argument>
  </argument>      
  <argument name="FIRST_SCHEDULE_JOB">
     <value></value>
  </argument>      
  <argument name="EMPLOYEE">
     <value></value>
  </argument>
</arguments>
<place placeType="JOB_SITE">
  <contact>
     <Name></Name>
     <Name_1></Name_1>
     <address>
        <street></street>
        <house_name></house_name>
        <zip></zip>
        <city></city>
        <province></province>
        <country></country>
     </address>
  </contact>
  <site>
     <street></street>
     <house_name></house_name>
     <zip></zip>
     <city></city>
     <province></province>
     <country></country>
  </site>            
 </place>

I want to loop through the elements in file B and fill the value from file A ie: The ZONE element (attribute ZONE) is filled with the ZONE 1 value.

I have tried with the XMLTextReader but hadn't had any luck so far:

while (emptyFile.Read())
{
    switch (emptyFile.NodeType)
    {
        case XmlNodeType.Element: // The node is an element.                                                        
           emptyFile.Name  = sourceFile.Name;

                         ........
     }
 }

Some help is greatly appreciated

Thanks

tanner
  • 11
  • 1
  • 3
  • 2
    Please include what you've already tried in your question - at the very least if shows people which technologies you're using to read the xml! – Liath Jan 20 '14 at 11:01
  • What seems to be the problem then? If you have your nodes, just write them to the other file. – Andrei V Jan 20 '14 at 11:02
  • possible duplicate of [How can I copy a block of XML from one document to the other?](http://stackoverflow.com/questions/12641206/how-can-i-copy-a-block-of-xml-from-one-document-to-the-other) – tom redfern Jan 20 '14 at 11:04
  • Also duplicate of http://stackoverflow.com/questions/16584641/copying-part-of-an-xml-document-to-another-document-using-xdocument – tom redfern Jan 20 '14 at 11:06
  • I have tried to use the XMLTextReader – tanner Jan 20 '14 at 12:35

1 Answers1

2

"LINQ to XML" makes it very easy to query & write to XML files. Have a look at:

http://msdn.microsoft.com/en-us/library/bb943906.aspx

http://msdn.microsoft.com/en-us/vstudio/bb688087.aspx

XElement root = XElement.Load("fileA.xml");

IEnumerable<XElement> address =
    from el in root.Elements("Address")
    where (string)el.Attribute("Type") == "Billing"
    select el;

foreach (XElement el in address)
    Console.WriteLine(el);
Fredrik
  • 2,247
  • 18
  • 21