如何从xml文件中检索,更新,追加和删除记录 [英] how to retrieve, Update, append and delete record from xml file
问题描述
Hello house;
我正在开发一个项目,要求我使用xml文件来保存用户的记录,我一直在保存记录,但仍然有问题追加新记录,删除记录,更新和检索记录,我在网上做了一些搜索,但没有成功。以下是我的代码:
Hello house;
I am working on a project that requires me to use xml file to save user's record, i have been to save the record, but still having issues appending a new record, deleting a record, updating and retrieving records, i have done a few searches online, but no success. Below is my code:
var writer = new XmlTextWriter( "XMLFile1.xml", System.Text.Encoding.UTF8 );
writer.WriteStartDocument( true );
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;
writer.WriteStartElement( "Table" );
createNode( "5", "Product 5", "5000", writer );
createNode( "6", "Product 6", "5000", writer );
createNode( "7", "Product 7", "5000", writer );
createNode( "9", "Product 8", "5000", writer );
writer.WriteEndElement( );
writer.WriteEndDocument( );
writer.Close( );
private void createNode(string pID, string pName, string pPrice, XmlTextWriter writer)
{
writer.WriteStartElement( "Product" );
writer.WriteStartElement( "Product_id" );
writer.WriteString( pID );
writer.WriteEndElement( );
writer.WriteStartElement( "Product_name" );
writer.WriteString( pName );
writer.WriteEndElement( );
writer.WriteStartElement( "Product_price" );
writer.WriteString( pPrice );
writer.WriteEndElement( );
writer.WriteEndElement( );
}
以上代码成功写入xml文件,我希望能够检索,追加新记录,更新并删除记录。任何帮助将不胜感激。在此先感谢。
The above codes writes successfully to xml file, i want o be able to retrieve, append new record, update and delete record. Any assistance will be greatly appreciated. Thanks in advance.
推荐答案
看看这个:
1. 插入-更新删除功能于XML的文件使用-C-夏普/ [< a href =http://www.c-sharpcorner.com/UploadFile/b8e86c/insert-update-delete-in-xml-file-using-C-Sharp/target =_ blanktitle =New Window > ^ ]
2. 如何附加到大型XML文件 [ ^ ]
Check this out:
1. insert-update-delete-in-xml-file-using-C-Sharp/[^]
2. How to Append to a Large XML File[^]
如果您使用LINQ
XDocument
和XElement
我认为这对你来说更容易。
If you useLINQ
withXDocument
andXElement
I think it is easier for you.
XElement xeTable = new XElement("Table");
XElement xeProducts = new XElement("Product");
createNode("5", "Product 5", "5000", xeProducts);
createNode("6", "Product 6", "5000", xeProducts);
createNode("7", "Product 7", "5000", xeProducts);
createNode("8", "Product 8", "5000", xeProducts);
xeTable.Add(xeProducts);
XDocument xdoc = new XDocument();
xdoc.Declaration = new XDeclaration("1.0", "utf8", "yes");
xdoc.Add(xeTable);
xdoc.Save(@"XMLFile1.xml");
private void createNode(string pID, string pName, string pPrice, XElement xeProducts)
{
XElement xeProduct = new XElement("Product");
xeProduct.Add(new XElement("Product_id", pID));
xeProduct.Add(new XElement("Product_name", pName));
xeProduct.Add(new XElement("Product_price", pPrice));
xeProducts.Add(xeProduct);
}
生成的XML
Resulting XML
<Products>
<Product>
<Product_id>5</Product_id>
<Product_name>Product 5</Product_name>
<Product_price>5000</Product_price>
</Product>
<Product>
<Product_id>6</Product_id>
<Product_name>Product 6</Product_name>
<Product_price>5000</Product_price>
</Product>
<Product>
<Product_id>7</Product_id>
<Product_name>Product 7</Product_name>
<Product_price>5000</Product_price>
</Product>
<Product>
<Product_id>8</Product_id>
<Product_name>Product 8</Product_name>
<Product_price>5000</Product_price>
</Product>
</Products>
< br $> b $ b
如何读取文件的示例:
Example of how to read the file:
XDocument xdoc2 = XDocument.Load(@"XMLFile1.xml");
XElement xeProducts2 = xdoc2.Root.Element("Products");
XElement xeProduct7 = xeProducts.Elements("Product").
Where(x => x.Element("Product_id").Value == "7").FirstOrDefault();
查询结果:
Result of the query:
<Product>
<Product_id>7</Product_id>
<Product_name>Product 7</Product_name>
<Product_price>5000</Product_price>
</Product>
访问会员
Access the members
string productName = xeProduct7.Element("Product_name").Value;
追加
Append
XDocument xdoc = XDocument.Load(@"XMLFile1.xml");
XElement xeProducts = xdoc.Root.Element("Products");
createNode("10", "Product 10", "10000", xeProducts);
xdoc.Save(@"XMLFile1.xml");
删除
Delete
XDocument xdoc = XDocument.Load(@"XMLFile1.xml");
XElement xeProducts = xdoc.Root.Element("Products");
XElement xeProduct7 = xeProducts.Elements("Product").
Where(x => x.Element("Product_id").Value == "7").FirstOrDefault();
xeProduct7.Remove();
xdoc.Save(@"XMLFile1.xml");
从这里你需要使用你自己的大脑la。
From here on you need to use your own brain la.
这篇关于如何从xml文件中检索,更新,追加和删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!