0

I have the following XML

<ROOT>
  <FSM338_Container>
    <FSM338_Details>
      <RunDate>2013-05-29 09:43:00</RunDate>
      <Uic>21690</Uic>
      <Date>2013-06-10 00:00:00</Date>
      <CASHBREAK>199</CASHBREAK>
      <CASHLUNCH>199</CASHLUNCH>
    </FSM338_Details>
    <FSM338_Details>
      <RunDate>2013-05-29 09:43:00</RunDate>
      <Uic>21690</Uic>
      <Date>2013-06-10 00:00:00</Date>
      <CASHBREAK>199</CASHBREAK>
      <CASHLUNCH>199</CASHLUNCH>
    </FSM338_Details>
  </FSM338_Container>
  <BillingReport>
    <RunDate>2013-05-29 09:43:00</RunDate>
    <Uic>21690</Uic>
    <Date>2013-06-10 00:00:00</Date>
    <gaindacd>1</gaindacd>
    <docnum>07000F</docnum>
  </BillingReport>
  <DataElements>
     <unitid>12345</unitid>
     <fbocost>0.00</fbo>
  </DataElements>
</ROOT>

I need to load the xml doc and add in several elements whenever I find the element named "Uic" . In short if I find "Uic" add in the element <someElement>my stuff here</someElement> at the same level as UIC at all locations.

I'Ve used

XmlDocument xDoc = new XmlDocument();
xDoc.Load(@"path_to_xml.xml");
list = xDoc.GetElementsByTagName("Uic");

I used insertBefore to add in my element but I can get it to copy to only the first element

2
  • Are you able to use LINQ to XML instead of XmlDocument? It would make it a lot easier. Commented Jun 18, 2013 at 18:52
  • 1
    Your Xml is malformed. Commented Jun 18, 2013 at 18:59

1 Answer 1

1

You can use SelectNodes() method of XmlNode which accepts xpath expression.

XmlNodeList nodes = xDoc.DocumentElement.SelectNodes("Uic");
foreach(XmlNode node in nodes) {
   XmlElement element = xDoc.CreateElement("SomeElement");
   element.InnerText = "anything";
   node.ParentNode.AppendChild(element);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.