?
Solved

Add Data to XML Sub-Root via C#?

Posted on 2011-09-19
6
Medium Priority
?
306 Views
Last Modified: 2012-05-12
Here's the XML tree. I need to add data to the "Links" node NOT the root.

<Settings>
    <Links>
    <Name>My Web Site</IpName>
    <Url>http://www.my.web.site.com/</IpUrl>
    </Links>
</Settings>

[Current code which alwayes writes to the Root]
 XmlDocument xmlDoc = new XmlDocument();
 xmlDoc.Load(file);
 XmlElement root = xmlDoc.DocumentElement;
 XmlElement ele = xmlDoc.CreateElement("Name");
 XmlText txt = xmlDoc.CreateTextNode(txtName.Text);
 root.AppendChild(ele);
 root.LastChild.AppendChild(txt);
 xmlDoc.Save(file);

0
Comment
Question by:pointeman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 12

Expert Comment

by:geowrian
ID: 36564459
You need to retrieve the "Links" node and then append "ele" and "txt" to to that (instead of the root). Without seeing how you are finding the root node, I'm not sure what would work best for you. That said, if you do:

XmlElement LinksElement = root.SelectSingleNode("Links");
LinksElement.AppendChild(ele); // replaces root.AppendChild(ele);
LinksElement.LastChild.AppendChild(txt); // replaces root.LastChild.AppendChild(txt);
0
 

Author Comment

by:pointeman
ID: 36566581
>>'finding root node'

A. XmlElement root = xmlDoc.DocumentElement; //default root of xml tree
0
 
LVL 12

Expert Comment

by:geowrian
ID: 36567572
Thanks. Then the lines of code I gave above should do the trick.
0
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 

Author Comment

by:pointeman
ID: 36569020
Yes, now the xml file looks like this:

<Settings>
    <Links>
       <Name>My Web Site</Name>
       <Url>http://www.my.web.site.com/</Url>
       <Name>Another Web</Name>
       <Url>www.another.web.com/</Url>
    </Links>
</Settings>

New Problem, cannot  read xml file as before, here's code:

ds = new DataSet();
ds.ReadXml(file);

List<KeyValuePair<string, string>> links = new List<KeyValuePair<string, string>>();

foreach (DataRow row in ds.Tables["Links"].Rows)
{
      links.Add(new KeyValuePair<string, string>(row["Name"].ToString(), row["Url"].ToString()));
}
0
 
LVL 12

Accepted Solution

by:
geowrian earned 2000 total points
ID: 36569320
I think you want your XML to look like this instead:

<Settings>
    <Links>
       <Name>My Web Site</Name>
       <Url>http://www.my.web.site.com/</Url>
    </Links>
    <Links>
       <Name>Another Web</Name>
       <Url>www.another.web.com/</Url>
    </Links>
</Settings>

In that case, you need to generate a new Links element for record, then attach the Name and Url children to that.
0
 

Author Comment

by:pointeman
ID: 36569396
Yea, XML is not my forte. I would still like the new records to insert at a certain point in the xml file, not just simply appending. I would like a more orderly insert.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question