Hello,
I am having a problem adding a node properly. It does add but it duplicates the Test node I know why it is doing it (I create the Test node in the root as well as in the dataset) but I don’t know how to prevent it. Any suggestions would be welcome as I have never used XML before. Thanks

<?xml version="1.0" ?> 
  <MyTest XMLVersion="1.3" Stamp="7/22/2009 11:55:59 PM">
  <Test>
    <Test>
      <MyTestID>bfc7941d-70a8-48c0-a535-561a73393457</MyTestID> 
      <MyTestID2>ccf2757f-63s5-57h9-d437-185s63285942</MyTestID2> 
      <SomeText>Some Text Here</SomeText>  
    </Test>
  </Test>  </OtherNode>

I want it to be (notice only one Test tag)

<?xml version="1.0" ?> 
  <MyTest XMLVersion="1.3" Stamp="7/22/2009 11:55:59 PM">
  <Test>
    <MyTestID>bfc7941d-70a8-48c0-a535-561a73393457</MyTestID> 
    <MyTestID2>ccf2757f-63s5-57h9-d437-185s63285942</MyTestID2> 
    <SomeText>Some Text Here</SomeText>  
  < /Test>  </OtherNode>

Here is the code I use to generate it:

string xml = "<?xml version=\"1.0\"?><MyTest XMLVersion=\"1.3\" Stamp=\"" + DateTime.Now.ToString() + "\"><Test></Test><OtherNode></OtherNode></MyTest>";
doc.LoadXml(xml);//start the xml 
GetTestData(); //gets dataset loads it into an XMLDocument called doc
TempNode = doc.SelectSingleNode("//MyTest");//get the 
myNode = m_myTest.SelectSingleNode("//Test");//get the node
myNode = m_MenuOnline.ImportNode(m_MyNode, true);
TempNode.AppendChild(myNode);//add the node

Thanks

Rather than adding the Temp node from the dataset as a child of the Temp node from doc, you could loop through the children of myNode and add them as children of TempNode.

string xml = "<?xml version=\"1.0\"?><MyTest XMLVersion=\"1.3\" Stamp=\"" + DateTime.Now.ToString() + "\"><Test></Test><OtherNode></OtherNode></MyTest>";
doc.LoadXml(xml);//start the xml 
GetTestData(); //gets dataset loads it into an XMLDocument called doc
TempNode = doc.SelectSingleNode("//MyTest");//get the 
myNode = m_myTest.SelectSingleNode("//Test");//get the node
myNode = m_MenuOnline.ImportNode(m_MyNode, true);
for(XmlNode child in myNode.ChildNodes)
    TempNode.AppendChild(child);//add the node

There's most likely other ways to do this, but it looks like it should solve your problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.