3 Replies - 13010 Views - Last Post: 17 July 2009 - 12:53 PM Rate Topic: -----

#1 conanlive  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-December 08

Modify XML Data in C#

Post icon  Posted 17 July 2009 - 11:05 AM

I am newbie in XML.
How to manipulate xml data in C#??

my xml file as shown below:

myfile.xml:

<project name="Hello"> 
<target name="XXX"> 
<exec program="PPP" failonerror="false"> 
<arg value="SSS" /> 
</exec> 
</target> 
<target name="YYY"> 
<exec program="QQQ" failonerror="false"> 
<arg value="aaa" /> 
<arg value="bbb" /> 
<arg value="ccc" /> 
</exec> 
</target> 
<target name="ZZZ"> 
<exec program="RRR" failonerror="false"> 
<arg value="SSS" /> 
<arg value="TTT" /> 
</exec> 
</target> 
</project> 


How to get expected output as shown below in C# with XmlDocument and XPath? Thanks!
<project name="Hello"> 
<target name="XXX"> 
<exec program="PPP" failonerror="false"> 
<arg value="SSS" /> 
</exec> 
</target> 
<target name="YYY"> 
<exec program="QQQ" failonerror="false"> 
<arg value="ddd" /> 
<arg value="eee" /> 
<arg value="aaa" /> 
<arg value="bbb" /> 
<arg value="ccc" /> 
<arg value="zzz" /> 
>arg value="yyy" /> 
</exec> 
</target> 
<target name="ZZZ"> 
<exec program="RRR" failonerror="false"> 
<arg value="SSS" /> 
<arg value="TTT" /> 
</exec> 
</target> 
</project>



Is This A Good Question/Topic? 0
  • +

Replies To: Modify XML Data in C#

#2 htsh_shrm  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 52
  • Joined: 18-October 07

Re: Modify XML Data in C#

Posted 17 July 2009 - 11:53 AM

What u need is all there in System.Xml namespace

first of all create a reader object specifying the path of ur file
and then in it use various methods and properties to retrieve each element with much ease

System.Xml.XmlReader reader=System.Xml.XmlReader.Create(new System.IO.FileStream("path of ur file"));

//then use methods and properties of reader object to read ur xml file


Was This Post Helpful? 0
  • +
  • -

#3 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Modify XML Data in C#

Posted 17 July 2009 - 12:07 PM

As I like to play around with XML because of it's efficiency and I don't handle it quite well yet, I decided to make an example program:


			// open new document
			string path = @"c:\temp\my.xml";
			XmlDocument doc = new XmlDocument();
			doc.Load(path);

			XmlNodeList nodes = doc.GetElementsByTagName("exec");

			// get the node which is appended with new nodes
			// in this case node with program="QQQ"
			XmlNode node = null;
			foreach (XmlNode n in nodes) {
				if (n.Attributes["program"].InnerText == "QQQ") {
					node = n;
					break;
				}
			}

			if (node != null) {
				XmlNodeList innerNodes = node.ChildNodes;

				// create necessary new elements
				XmlNode newNode1 = doc.CreateElement("arg");
				XmlAttribute newAtt1 = doc.CreateAttribute("value");
				newAtt1.Value = "ddd";
				newNode1.Attributes.Append(newAtt1);

				node.InsertBefore(newNode1, node.FirstChild);

				// ... 
			}

			// save document
			doc.Save(path);



And after testing that code, the results are:

<project name="Hello">
  <target name="XXX">
    <exec program="PPP" failonerror="false">
      <arg value="SSS" />
    </exec>
  </target>
  <target name="YYY">
    <exec program="QQQ" failonerror="false">
      <arg value="ddd" />
      <arg value="aaa" />
      <arg value="bbb" />
      <arg value="ccc" />
    </exec>
  </target>
  <target name="ZZZ">
    <exec program="RRR" failonerror="false">
      <arg value="SSS" />
      <arg value="TTT" />
    </exec>
  </target>
</project>



Now it's your job to do the rest.

This post has been edited by janne_panne: 17 July 2009 - 12:13 PM

Was This Post Helpful? 0
  • +
  • -

#4 Aeternalis  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 291
  • Joined: 13-July 09

Re: Modify XML Data in C#

Posted 17 July 2009 - 12:53 PM

If your looking for the coded solution to this problem, move along.. nothing to see here..

However, I might be able to help by explaining a few things about the problem you present. Then, hopefully you will be able to code the answer yourself. Start by checking out

http://msdn.microsof...ent(VS.71).aspx
http://msdn.microsof...ers(VS.71).aspx

Armed with the class definition and documentation of the member methods, I would try to write some pseudo code that would do what I wanted. In this example we are looking to:

1. construct a new node of type <arg>
2. set the value attribute of that node
3. add the created node to the <exec> node of a <target> node.
4. rinse and repeat...


[edit] after reviewing this further.. there is a tutorial on Microsoft support that shows how to do this better than I can..

http://support.microsoft.com/kb/318499

I hope this helps.. XML is a great skill to have!

Aeternalis
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1