XML:
<servers ver="1" namespace="RWT.Direct.Processors.Public.ROCTP">
<procedures>
<procedure name="Func1" returnType="">
<params>
<param name="p1" type="int" />
<param name="p2" type="int" keyword="out" />
</params>
</procedure>
<procedure name="Func2" returnType="int">
<params>
<param name="x" type="int" />
</params>
</procedure>
</procedures>
</servers>
我的代码:
FileStream fs = new FileStream(@"D:\DIRECT\bin\base\Servers\Test.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader sr = new StreamReader(fs);
var doc = XDocument.Parse(sr.ReadToEnd());
List<Procedure> procedures = (from _api in doc.Element("servers").Descendants("procedures")
select new Procedure
{
Name = _api.Element("procedure").Attribute("name").Value,
ReturnType = _api.Element("procedure").Attribute("returnType").Value
}).ToList();
foreach (var item in procedures)
{
Console.WriteLine(item.Name + " " + item.ReturnType);
}
我只回到了“Func1”。
答案 0 :(得分:2)
您只获得一个结果,因为您正在查询名为“Procedures”的元素,该元素仅在您的文档中出现过一次。检查此查询:
List<Procedure> procedures = (from _api in doc.Element("servers").Descendants("procedure")
select new Procedure
{
Name = _api.Attribute("name").Value,
ReturnType = _api.Attribute("returnType").Value
}).ToList();
foreach (var item in procedures)
{
Console.WriteLine(item.Name + " " + item.ReturnType);
foreach (var item1 in item.ParamList)
{
Console.WriteLine("\t" + item1.Name + " " + item1.Type + " " + item1.Keyword);
}
}
但是,您尝试获取的item.ParamList
永远不会被您的查询设置!