Solved

Add Data to XML Sub-Root via C#?

Posted on 2011-09-19
6
302 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
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 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