0

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”回来。

4

1 回答 1

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您的查询从未设置的内容!

于 2013-03-09T13:04:08.693 回答