小编use*_*428的帖子

LINQ Xelement当子节点存在时返回null

我有一个像这样的XML文件,我想读取ID,短名称,名称节点值.

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
<AccountingUnitList xmlns="http://www.google.com">
<AccountingUnit>
<ID>17406</ID> 
<ShortName>test</ShortName> 
<Name>test</Name> 
</AccountingUnit>
<AccountingUnit>
<ID>18006</ID> 
<ShortName>ANOTHERtest</ShortName> 
<Name>Anothertest</Name> 
</AccountingUnit>
<AccountingUnit>
<ID>18046</ID> 
<ShortName>RKU</ShortName>
<Name>hospital</Name> 
</AccountingUnit>
<AccountingUnit>
<ID>18047</ID> 
<ShortName>MNU</ShortName> 
<Name>MNU</Name> 
</AccountingUnit>
</AccountingUnitList>
Run Code Online (Sandbox Code Playgroud)

递归读取Node元素的最佳方法是什么?

这就是我尝试读取Node值的方法:

var accountingunit = ( 
                from e in XDocument.Parse(textresult).Root.Elements("AccountingUnit")
                select new node
                {
                     idvalue = (string)e.Element("ID"),
                     shortname =(string)e.Element("ShortName"),
                     name = (string)e.Element("Name"),

                });

            foreach(var unit in accountingunit)
            {
               Console.WriteLine("ID"+ unit.idvalue + unit.name + unit.shortname);
            }
Run Code Online (Sandbox Code Playgroud)

这是节点consructor:

public class node
{
    public string idvalue { get; set; }
    public string shortname { …
Run Code Online (Sandbox Code Playgroud)

c# xml linq-to-xml

6
推荐指数
1
解决办法
3718
查看次数

标签 统计

c# ×1

linq-to-xml ×1

xml ×1