469,338 Members | 8,424 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

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 53741
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
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.