How can I read and edit an XML file using C #?

How to open and edit an existing XML file? I would like to change some values, for example:

<address>myaddr</address>

For example, I would like to put loreal instead of myaddr. I work in C #. I would appreciate it if you could show me the code.

+3
source share
3 answers

You can use the XDocument class :

var doc = XDocument.Load("test.xml");
var address = doc.Root.Element("address");
if (address != null)
{
    address.Value = "new value";
}
doc.Save("test.xml");
+6
source

Yes, it is quite possible - and quite easy.

Read these resources:

- "Intro Linq-to-XML" "Intro XMLDocument" - .

+1

, XML:

<root>
    <address>myaddr</address>
</root>

. . XML, XML , XML (). :

XDocument doc = XDocument.Load("myfile.xml");
doc.Root.Element("address").Value = "new address"
doc.Save("myfile.xml")

:

+1

Source: https://habr.com/ru/post/1794067/


All Articles