XML操作求助 - Linq to xml 不熟
本帖最后由 tabz 于 2017-5-23 10:11 编辑<?xml version="1.0" encoding="utf-8"?>
<PathList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Path>
<PathName>PM_CableLoss</PathName>
<PathID>1</PathID>
<AdditionalLoss>0</AdditionalLoss>
<Cal_Date>4/7/2010 3:58 PM</Cal_Date>
<TesterID>TesterA</TesterID>
<CalCable_PathName>CAL_CABLE</CalCable_PathName>
<UseCalCable>true</UseCalCable>
<DataList>
<Data>
<Frequency>2412</Frequency>
<Value>10.7</Value>
<Delta>0.0</Delta>
</Data>
<Data>
<Frequency>2417</Frequency>
<Value>10.7</Value>
<Delta>0.0</Delta>
</Data>
<Data>
<Frequency>2422</Frequency>
<Value>10.8</Value>
<Delta>0.0</Delta>
</Data>
<Data>
<Frequency>2427</Frequency>
<Value>10.9</Value>
<Delta>0.0</Delta>
</Data>
</DataList>
</Path>
<Path>
<PathName>Test_CableLoss</PathName>
<PathID>2</PathID>
<AdditionalLoss>0</AdditionalLoss>
<Cal_Date>4/7/2010 3:58 PM</Cal_Date>
<TesterID>TesterB</TesterID>
<CalCable_PathName>CAL_CABLE</CalCable_PathName>
<UseCalCable>true</UseCalCable>
<DataList>
<Data>
<Frequency>2412</Frequency>
<Value>0</Value>
<Delta>0.0</Delta>
</Data>
<Data>
<Frequency>2417</Frequency>
<Value>0</Value>
<Delta>0.0</Delta>
</Data>
<Data>
<Frequency>2422</Frequency>
<Value>0</Value>
<Delta>0.0</Delta>
</Data>
<Data>
<Frequency>2427</Frequency>
<Value>0</Value>
<Delta>0.0</Delta>
</Data>
<Data>
<Frequency>2432</Frequency>
<Value>0</Value>
<Delta>0.0</Delta>
</Data>
</DataList>
</Path>
</PathList>
上面是要操作的XML,下面是我自己的代码。
XmlDocument xml = new XmlDocument();
xml.Load("loss.xml");
XmlNodeList xnl = xml.SelectNodes("/PathList/Path/DataList/Data");
foreach (XmlNode x in xnl)
{
string firstName = x["Frequency"].InnerText;
string lastName = x["Value"].InnerText;
Console.WriteLine("{0}\t {1}", firstName, lastName);
}
问题是我不想一次遍历出所有结果,我想按 <PathName>PM_CableLoss</PathName>这个字段分别遍历,知道有好的方法,但一直想不出,对Linq to xml也不熟,求大神相助,谢谢!
页:
[1]