943,757 Members | Top Members by Rank

Ad:
Jul 23rd, 2009
0

XML help with tags

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
smithsf22 is offline Offline
6 posts
since Mar 2008
Jul 23rd, 2009
0

Re: XML help with tags

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.

C# 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. 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.
Reputation Points: 23
Solved Threads: 10
Light Poster
bcasp is offline Offline
45 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in XML, XSLT and XPATH Forum Timeline: I'm way out of my depth and need desperate help.
Next Thread in XML, XSLT and XPATH Forum Timeline: Blog Aggregator: XML and XSLT





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC