472,090 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,090 software developers and data experts.

How to update XML file?

I have a XML file as following:
<configuration>
<MachineList>
<Machine Name="mycomputer"/>
</MachineList>
</configuration>

How can I program to change "mycomputer" to "somecomputer"? Any
comments will be appreciated.

May 25 '07 #1
4 53970
On May 25, 8:50 am, Hooyoo <zhao_huy...@126.comwrote:
I have a XML file as following:
<configuration>
<MachineList>
<Machine Name="mycomputer"/>
</MachineList>
</configuration>

How can I program to change "mycomputer" to "somecomputer"? Any
comments will be appreciated.
Hi,

Use XmlDocument to load the xml and then navigate through it to find
the element that contains the value you want to change.

System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
xmlDocument.Load(<filename or stream>);

Now, choose the most suitable method for you to find the elements or
attributes you want to change:

// For example:
XmlNodeList elemList = xmlDocument .GetElementsByTagName("Machine");
for (int i=0; i < elemList.Count; i++)
{
elemList[i].InnerXml = "somecomputer";
}

Then, call the Save method to save the changes.

xmlDocument.Save(...);

Hope this helps,
Moty

May 25 '07 #2
If you want to preserve white spaces and indentation when saving, use
this technique:

1. After creating the xml document, set its PreserveWhitespace
property to true:

System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
xmlDocument.PreserveWhitespace = true; //this preserves white spaces
so that they won't be eliminated when loading the document

2. Instead of simply calling xmlDocument.Save(...), replace
xmlDocument.Save(...) with the following lines of code:

XmlTextWriter tw = new XmlTextWriter(<filename>, Encoding.ASCII);
try {
tw.Formatting = Formatting.Indented; //this preserves indentation
xmlDocument.Save(tw);
}
finally {
tw.Close();
}

I'm frequently using this method as I'm manipulating xml document that
should remain on disk and should be easily readable by a human user
that uses a simple text editor/viewer.

Hope this helps too
Moty Michaely a scris:
On May 25, 8:50 am, Hooyoo <zhao_huy...@126.comwrote:
I have a XML file as following:
<configuration>
<MachineList>
<Machine Name="mycomputer"/>
</MachineList>
</configuration>

How can I program to change "mycomputer" to "somecomputer"? Any
comments will be appreciated.

Hi,

Use XmlDocument to load the xml and then navigate through it to find
the element that contains the value you want to change.

System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
xmlDocument.Load(<filename or stream>);

Now, choose the most suitable method for you to find the elements or
attributes you want to change:

// For example:
XmlNodeList elemList = xmlDocument .GetElementsByTagName("Machine");
for (int i=0; i < elemList.Count; i++)
{
elemList[i].InnerXml = "somecomputer";
}

Then, call the Save method to save the changes.

xmlDocument.Save(...);

Hope this helps,
Moty
May 25 '07 #3
On May 25, 2:18 pm, Moty Michaely <Moty...@gmail.comwrote:
On May 25, 8:50 am, Hooyoo <zhao_huy...@126.comwrote:
I have a XML file as following:
<configuration>
<MachineList>
<Machine Name="mycomputer"/>
</MachineList>
</configuration>
How can I program to change "mycomputer" to "somecomputer"? Any
comments will be appreciated.

Hi,

Use XmlDocument to load the xml and then navigate through it to find
the element that contains the value you want to change.

System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
xmlDocument.Load(<filename or stream>);

Now, choose the most suitable method for you to find the elements or
attributes you want to change:

// For example:
XmlNodeList elemList = xmlDocument .GetElementsByTagName("Machine");
for (int i=0; i < elemList.Count; i++)
{
elemList[i].InnerXml = "somecomputer";

}

Then, call the Save method to save the changes.

xmlDocument.Save(...);

Hope this helps,
Moty
Thank you very much. Your method works. But you made a mistake:
In my sample xml file:

elemList[i].InnerXml = "somecomputer";
should be
elemList[0].Attributes[0].InnerText = "SomeComputer;

Anyway, I appreciate your warm-heart help. ^_^.
May 25 '07 #4
On May 25, 10:49 am, Hooyoo <zhao_huy...@126.comwrote:
On May 25, 2:18 pm, Moty Michaely <Moty...@gmail.comwrote:
On May 25, 8:50 am, Hooyoo <zhao_huy...@126.comwrote:
I have a XML file as following:
<configuration>
<MachineList>
<Machine Name="mycomputer"/>
</MachineList>
</configuration>
How can I program to change "mycomputer" to "somecomputer"? Any
comments will be appreciated.
Hi,
Use XmlDocument to load the xml and then navigate through it to find
the element that contains the value you want to change.
System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
xmlDocument.Load(<filename or stream>);
Now, choose the most suitable method for you to find the elements or
attributes you want to change:
// For example:
XmlNodeList elemList = xmlDocument .GetElementsByTagName("Machine");
for (int i=0; i < elemList.Count; i++)
{
elemList[i].InnerXml = "somecomputer";
}
Then, call the Save method to save the changes.
xmlDocument.Save(...);
Hope this helps,
Moty

Thank you very much. Your method works. But you made a mistake:
In my sample xml file:

elemList[i].InnerXml = "somecomputer";
should be
elemList[0].Attributes[0].InnerText = "SomeComputer;

Anyway, I appreciate your warm-heart help. ^_^.
Hi,
Yep, you are right.

Any time!

Have a great day,
Moty

May 25 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Michael Reynolds | last post: by
5 posts views Thread by anas.hashmi | last post: by
1 post views Thread by EAI | last post: by
reply views Thread by Dmitry Teslenko | last post: by
reply views Thread by Gabriel Genellina | last post: by
5 posts views Thread by CSharper | last post: by
ddtpmyra
4 posts views Thread by ddtpmyra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.