How to update XML file?

  • Thread starter Thread starter Hooyoo
  • Start date Start date
H

Hooyoo

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.
 
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.InnerXml = "somecomputer";
}

Then, call the Save method to save the changes.

xmlDocument.Save(...);

Hope this helps,
Moty
 
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:
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.InnerXml = "somecomputer";
}

Then, call the Save method to save the changes.

xmlDocument.Save(...);

Hope this helps,
Moty
 
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.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.InnerXml = "somecomputer";
should be
elemList[0].Attributes[0].InnerText = "SomeComputer;

Anyway, I appreciate your warm-heart help. ^_^.
 
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.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.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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top