Lea*_*any 3 .net c# xml c#-4.0
我在.NET中学习Xml数据处理.我有以下XML格式.
<BOOKS>
<BOOK>
<TITLE>book 1</TITLE>
<AUTHOR>author 1</AUTHOR>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</BOOK>
<BOOK>
<TITLE>book 2</TITLE>
<AUTHOR>author 2</AUTHOR>
<PRICE>20.90</PRICE>
<YEAR>1995</YEAR>
</BOOK>
</BOOKS>
Run Code Online (Sandbox Code Playgroud)
我需要学习添加/编辑/删除新书到XML文件.能否请您指导我们探索这些功能的所有课程.我发现许多类XmlDocument XmlTextWriter等等.一些网站建议也使用LINQ.我很困惑哪个去了.有什么好的材料我可以参考了解这一点.
以下是使用LINQ to XML添加和删除元素的示例:
// load the XML file into an XElement
XElement xml = XElement.Load(filePath);
// add a new book
xml.Add(
new XElement("BOOK",
new XElement("TITLE", "book 3"),
new XElement("AUTHOR", "author 3"),
new XElement("PRICE", 0.1),
new XElement("YEAR", 2012)));
// remove a book that matches the desired title
xml.Elements("BOOK").Where(x => x.Element("TITLE").Value == "book 1").Remove();
// to edit an existing element:
xml.Elements("BOOK")
.Single(x => x.Element("TITLE").Value == "book 2") // take a single book
.Element("AUTHOR").Value = "new author"; // and change its author field
Run Code Online (Sandbox Code Playgroud)
基本上,只要您对该技术感到满意,就可以使用您想要的任何东西.在我看来,LINQ to SQL似乎更容易一些.
| 归档时间: |
|
| 查看次数: |
2664 次 |
| 最近记录: |