我有一个如下所示的xml,因为我想得到第二个SubNetwork
元素值和tval
值,所以这里的第一行值为NC22
和tet_DS_U_6359_HolidayExpressInKV
。我按照如下所示的prtial代码进行了操作,如何获取每行的第二个SubNetwork
和tval
值,最后我将放入tval
是关键字的dictionery。这个很大几GB大小的XML,我发布了一小部分
(XmlReader xr = XmlReader.Create(path))
{
string sitename = "";
xr.MoveToContent();
XNamespace xn = xr.LookupNamespace("tn");
while (xr.Read())
{
while (xr.NodeType == XmlNodeType.Element && xr.NamespaceURI == tn && xr.LocalName == "mentNode" && xr.GetAttribute("id") == "TRM")
{
try
{
XElement sniipet = (XElement)XNode.ReadFrom(xr);
var xdoc = new XDocument(sniipet);
var orders = (from r in xdoc.Descendants(tn + "manages")
select new
{
}).ToList();
}
catch (Exception ex)
{
}
}
}
}
和XML如下所示
<?xml version="1.0" encoding="utf-8" ?>
<tn:mentNode id="TRM">
<tn:attributes>
<tn:manages>SubNetwork=TRM_R,SubNetwork=NC22,tval=tet_DS_U_6359_HolidayExpressInKV,Element=1</tn:manages>
<tn:manages>SubNetwork=TRM_R,SubNetwork=NC22,tval=tet_DS_U_6352_MediaOneHtl,Element=1</tn:manages>
</tn:attributes>
</tn:mentNode>
修改
虽然XML很大<tn:mentNode id="TRM">
节点并不是我在dictionery中可以容纳的那么多
答案 0 :(得分:2)
您只能使用XDocument而不是XmlReader:
XDocument xr = XDocument.Load(path);
List<string> orders = xr.Descendants().Where(x => (x.Name.LocalName == "mentNode") && ((string)x.Attribute("id") == "TRM"))
.Select(x => x.Descendants().Where(y => y.Name.LocalName == "manages").Select(y => (string)y))
.SelectMany(x => x).ToList();