Solved

Add Data to XML Sub-Root via C#?

Posted on 2011-09-19
6
263 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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!
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now