473,503 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 54081
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1805
by: Michael Reynolds | last post by:
I can read file attributes just fine (author, title, etc,) But there's no way to update them. I know windows application can do it, but can a web application update these files? Here's the...
5
17913
by: anas.hashmi | last post by:
I am trying to write to the beginning of a file. The reason: I want to make a form where board webmasters can use it to insert in updates to a webpage without having to go directly into the web...
2
2268
by: Arjen | last post by:
Hi, Can someone help me to update my XML file? This is what I have. XmlDoc.Load(MapPath("~/myFile.xml")); XmlNodeList oXmlUser = oXmlDoc.GetElementsByTagName("user"); foreach (XmlNode user...
1
1553
by: EAI | last post by:
Hi All, I have a requirement to update first 200 bytes of a huge file. I know this can be done in a couple of ways. But all of those require a lot of processing. What would be the efficient way...
0
954
by: Dmitry Teslenko | last post by:
Hello! I use some script in python 2.5 from vim editor (it has python bindings) that updates some file and then launches another program (ms visual studio, for example) to do something with...
0
1494
by: Gabriel Genellina | last post by:
En Tue, 13 May 2008 11:57:03 -0300, Dmitry Teslenko <dteslenko@gmail.com> escribió: Is the code above contained in a function? So all references are released upon function exit? If not, you...
5
1681
by: CSharper | last post by:
I have a data file which is 2.1GB (zipped), when the exe runs, it copies the content of the zip file to a destination folder. What is the best way to achive this? Current implementation is to zip...
4
6284
ddtpmyra
by: ddtpmyra | last post by:
Im having trouble on updating the file inside mysql database using the php. Should I use <form> or im wrong on how I called the variable from previous page? FORM 1 - that accepts the file pointer...
7
1632
by: Jashua Cumbie | last post by:
I have recently imported some data to SQL for a client, using files provided by another company. We made edits in Excel, using different vlookups and concatenates, and have now realized that one...
1
2463
HaLo2FrEeEk
by: HaLo2FrEeEk | last post by:
I've written an update method for one of my programs and for the most part it works well, but there is a slight issue. The way I have it check for updates is to simply download an XML file from my...
0
7201
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7083
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
5011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3166
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1510
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.