<?xml version="1.0" encoding="utf-8" ?>
<UserConment>
<News>
<ID>1786</ID>
<Conment>这个很好
</Conment>
</News>
<News>
<ID>1968</ID>
<Conment>
我认为这个还有待改进
</Conment>
</News>
<News>
<ID>1786</ID>
<Conment>
这个不行 效果不好
</Conment>
</News>
... ... ...
... ... ...
</UserConment>
我要根据ID号读取某个ID的所有结点信息,这个XML文件数据量很大,采用什么什么方式?XmlReader只能读,XmlDocument使用XPath很慢,那么采用什么解决方法好呢?
<UserConment>
<News>
<ID>1786</ID>
<Conment>这个很好
</Conment>
</News>
<News>
<ID>1968</ID>
<Conment>
我认为这个还有待改进
</Conment>
</News>
<News>
<ID>1786</ID>
<Conment>
这个不行 效果不好
</Conment>
</News>
... ... ...
... ... ...
</UserConment>
我要根据ID号读取某个ID的所有结点信息,这个XML文件数据量很大,采用什么什么方式?XmlReader只能读,XmlDocument使用XPath很慢,那么采用什么解决方法好呢?
XmlDocument document = new XmlDocument();
document.Load(@"path");
XmlNode basicnode = document.DocumentElement;
if (basicnode.Name == "UserConment")
{
foreach (XmlNode node in basicnode.ChildNodes)
{
if (node.Name == "News")
{
foreach (XmlNode cnode in node.ChildNodes)
{
if (cnode.Name == "Id" && cnode.InnerText == "1786")
{
ID = cnode.InnerText;
}
if (cnode.Name == "Conment")
{
Conment = cnode.InnerText;
}
}
}
} }
写同样. if (cnode.Name == "Id" && cnode.InnerText == "1786")
{
cnode.InnerText = ID;
}
在后面加个.
document.Save(path)就可以了.
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml; namespace WindowsApplication211
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); XmlDocument Doc = new XmlDocument();
Doc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
+ "<UserConment>"
+ "<News>"
+ "<ID>1786</ID>"
+ "<Conment>这个很好"
+ "</Conment>"
+ "</News>"
+ "<News>"
+ "<ID>1968</ID>"
+ "<Conment>"
+ "我认为这个还有待改进"
+ "</Conment>"
+ "</News>"
+ "</UserConment>"); XmlNode Node=Doc.SelectSingleNode("//UserConment/News/ID[.=\"1786\"]");
MessageBox.Show(Node.ChildNodes[0].InnerText);
}
}
}
foreach (XmlNode cnode in node.ChildNodes)
{
if (cnode.Name == "Id" && cnode.InnerText != "1786")
{
continue;
}
if(code.Name="Id")
{
ID = cnode.InnerText; }
if (cnode.Name == "Conment")
{
Conment = cnode.InnerText;
}
}
.Where(u => u.Element("ID").Value == "1723")
select p;
XElement xel1 = result.FirstOrDefault();