XML help with tags

Please support our XML, XSLT and XPATH advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 6
Reputation: smithsf22 is an unknown quantity at this point 
Solved Threads: 0
smithsf22 smithsf22 is offline Offline
Newbie Poster

XML help with tags

 
0
  #1
Jul 23rd, 2009
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, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <?xml version="1.0" ?>
  2. <MyTest XMLVersion="1.3" Stamp="7/22/2009 11:55:59 PM">
  3. <Test>
  4. <Test>
  5. <MyTestID>bfc7941d-70a8-48c0-a535-561a73393457</MyTestID>
  6. <MyTestID2>ccf2757f-63s5-57h9-d437-185s63285942</MyTestID2>
  7. <SomeText>Some Text Here</SomeText>
  8. </Test>
  9. </Test> </OtherNode>

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

XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <?xml version="1.0" ?>
  2. <MyTest XMLVersion="1.3" Stamp="7/22/2009 11:55:59 PM">
  3. <Test>
  4. <MyTestID>bfc7941d-70a8-48c0-a535-561a73393457</MyTestID>
  5. <MyTestID2>ccf2757f-63s5-57h9-d437-185s63285942</MyTestID2>
  6. <SomeText>Some Text Here</SomeText>
  7. < /Test> </OtherNode>

Here is the code I use to generate it:

XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. string xml = "<?xml version=\"1.0\"?><MyTest XMLVersion=\"1.3\" Stamp=\"" + DateTime.Now.ToString() + "\"><Test></Test><OtherNode></OtherNode></MyTest>";
  2. doc.LoadXml(xml);//start the xml
  3. GetTestData(); //gets dataset loads it into an XMLDocument called doc
  4. TempNode = doc.SelectSingleNode("//MyTest");//get the
  5. myNode = m_myTest.SelectSingleNode("//Test");//get the node
  6. myNode = m_MenuOnline.ImportNode(m_MyNode, true);
  7. TempNode.AppendChild(myNode);//add the node

Thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 45
Reputation: bcasp is an unknown quantity at this point 
Solved Threads: 10
bcasp bcasp is offline Offline
Light Poster

Re: XML help with tags

 
0
  #2
Jul 23rd, 2009
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.

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

There's most likely other ways to do this, but it looks like it should solve your problem.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the XML, XSLT and XPATH Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC