Solved

Add Data to XML Sub-Root via C#?

Posted on 2011-09-19
6
290 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
  • 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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