C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » XML »

Nesting in xml


Posted Date: 14 Oct 2009      Posted By: khan asif      Member Level: Bronze     Points: 1   Responses: 1



hi folks.,
i have a code is like this..

xmlDoc.Load(strFilename);

XmlElement elmRoot = xmlDoc.DocumentElement;
elmNew = xmlDoc.CreateElement("Ping");
elmRoot.AppendChild(elmNew);


elmNew = xmlDoc.CreateElement("Settings");
elmRoot.AppendChild(elmNew);


elmNew = xmlDoc.CreateElement("Name");
txt = xmlDoc.CreateTextNode(nameTextBox.Text);
elmRoot.LastChild.AppendChild(elmNew);
elmRoot.LastChild.LastChild.AppendChild(txt);

elmNew = xmlDoc.CreateElement("URL");
txt = xmlDoc.CreateTextNode(urlTextBox.Text);
elmRoot.LastChild.AppendChild(elmNew);
elmRoot.LastChild.LastChild.AppendChild(txt);

elmNew = xmlDoc.CreateElement("TriggerDocumentType");
txt = xmlDoc.CreateTextNode(tdtTextBox.Text);
elmRoot.LastChild.AppendChild(elmNew);
elmRoot.LastChild.LastChild.AppendChild(txt);

elmNew = xmlDoc.CreateElement("Services");
elmRoot.AppendChild(elmNew);
string serv = serviceTextBox.Text;
string[] myArray = serv.Split(new char[] { ',' });
for (i = 0; i < myArray.Length; i++)
{
//this will iterate through your array and put the current value in a string for manipulation (database insert?)
result = myArray[i];
elmNew = xmlDoc.CreateElement("Service");
txt = xmlDoc.CreateTextNode(result);
elmRoot.LastChild.AppendChild(elmNew);
elmRoot.LastChild.LastChild.AppendChild(txt);
}


i want output as like this--

<Pinger>

<ping>
<Settings>
<Name>testing</Name>
<URL>testing</URL>
<TriggerDocumentType>rstinf</TriggerDocumentType>
</Settings>
<Services>
<Service>www.google.com</Service>
<Service>www.msn.com</Service>
</Services>
</ping>
</Pinger>

the problem here is <ping>element is not nesting properly..
any pointers?





Responses

Author: Chandra Shekar.Y    15 Oct 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

Hi

You need to create a new xml element object for each new create element method rather using the existing one(elemNew)
eg:

XmlElement pingElement = xmlDoc.CreateElement("Ping");
xmlDoc.DocumentElement.AppendChild(pingElement);

XmlElement settingElement = xmlDoc.CreateElement("Settings");
pingElement.AppendChild(settingElement); // This will come to as a child element to ping element.

and so on for other elements.



Post Reply
You must Sign In to post a response.
Next : Serialization
Previous : Read xml
Return to Discussion Forum
Post New Message
Category: XML

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use