How can I open and edit an existing XML file? I would like to modify some values like:
如何打开和编辑现有XML文件?我想修改一些值,如:
<address>myaddr</address>
For example, I would like to put loreal instead if myaddr. I am working in C#. I would appreciate if you can show me some code.
例如,如果myaddr,我想放置loreal。我在C#工作。如果你能告诉我一些代码,我将不胜感激。
3 个解决方案
#1
7
You could use the XDocument class:
您可以使用XDocument类:
var doc = XDocument.Load("test.xml");
var address = doc.Root.Element("address");
if (address != null)
{
address.Value = "new value";
}
doc.Save("test.xml");
#2
2
Let's say you have the following XML file:
假设您有以下XML文件:
<root>
<address>myaddr</address>
</root>
And you want to do the replacement. There are many options. Some are explicitly modifying XML, others are translating your XML to classes, modifying and translating back to XML (serialization). Here is one of the ways do do it:
你想做替换。有很多选择。一些是显式修改XML,另一些是将XML转换为类,修改和转换回XML(序列化)。以下是其中一种方法:
XDocument doc = XDocument.Load("myfile.xml");
doc.Root.Element("address").Value = "new address"
doc.Save("myfile.xml")
For more information read the following:
有关更多信息,请阅读以下内容
-
LINQ to XML is the technique I used here - http://msdn.microsoft.com/en-us/library/bb387098.aspx
LINQ to XML是我在这里使用的技术 - http://msdn.microsoft.com/en-us/library/bb387098.aspx
-
XML serialization is another technique - http://msdn.microsoft.com/en-us/library/182eeyhh.aspx
XML序列化是另一种技术 - http://msdn.microsoft.com/en-us/library/182eeyhh.aspx
#3
1
Yes, that's totally possible - and quite easily, too.
是的,这完全有可能 - 而且很容易。
Read those resources:
阅读这些资源:
- Introduction to XML Document Object Model
- Introduction to Linq-to-XML: simple XML parsing
XML文档对象模型简介
Linq-to-XML简介:简单的XML解析
and a great many more - just search for "Intro Linq-to-XML" or "Intro XMLDocument" - you'll get plenty of links to good articles and blog posts.
还有更多 - 只需搜索“Intro Linq-to-XML”或“Intro XMLDocument” - 您将获得大量链接到好文章和博客文章。
#1
7
You could use the XDocument class:
您可以使用XDocument类:
var doc = XDocument.Load("test.xml");
var address = doc.Root.Element("address");
if (address != null)
{
address.Value = "new value";
}
doc.Save("test.xml");
#2
2
Let's say you have the following XML file:
假设您有以下XML文件:
<root>
<address>myaddr</address>
</root>
And you want to do the replacement. There are many options. Some are explicitly modifying XML, others are translating your XML to classes, modifying and translating back to XML (serialization). Here is one of the ways do do it:
你想做替换。有很多选择。一些是显式修改XML,另一些是将XML转换为类,修改和转换回XML(序列化)。以下是其中一种方法:
XDocument doc = XDocument.Load("myfile.xml");
doc.Root.Element("address").Value = "new address"
doc.Save("myfile.xml")
For more information read the following:
有关更多信息,请阅读以下内容
-
LINQ to XML is the technique I used here - http://msdn.microsoft.com/en-us/library/bb387098.aspx
LINQ to XML是我在这里使用的技术 - http://msdn.microsoft.com/en-us/library/bb387098.aspx
-
XML serialization is another technique - http://msdn.microsoft.com/en-us/library/182eeyhh.aspx
XML序列化是另一种技术 - http://msdn.microsoft.com/en-us/library/182eeyhh.aspx
#3
1
Yes, that's totally possible - and quite easily, too.
是的,这完全有可能 - 而且很容易。
Read those resources:
阅读这些资源:
- Introduction to XML Document Object Model
- Introduction to Linq-to-XML: simple XML parsing
XML文档对象模型简介
Linq-to-XML简介:简单的XML解析
and a great many more - just search for "Intro Linq-to-XML" or "Intro XMLDocument" - you'll get plenty of links to good articles and blog posts.
还有更多 - 只需搜索“Intro Linq-to-XML”或“Intro XMLDocument” - 您将获得大量链接到好文章和博客文章。