【问题标题】:How to use XMLReader to parse XML document? [closed]如何使用 XMLReader 解析 XML 文档? [关闭]
【发布时间】:2013-12-10 20:52:11
【问题描述】:

这是我的 XML:

http://simon.ist.rit.edu:8080/Services/resources/ESD/OrgTypes/

我希望能够抓住“类型”下的那些键盘并忽略数字。

如医师、救护车等

<data>
<row>
<typeId>0</typeId>
<type>Physician</type>
</row>
<row>
<typeId>1</typeId>
<type>Ambulance</type>
</row>
<row>
<typeId>2</typeId>
<type>Fire Department</type>
</row>
<row>
<typeId>3</typeId>
<type>Helicopter/Air Transport</type>
</row>
</data>

我将如何在 C# 中做到这一点?

【问题讨论】:

  • 你试过了吗?
  • 我会作弊,在内存中创建一个 System.Data.DataTable 对象,使用 DataTable.ReadXML 函数,然后将它放在一个方便易用的 DataTable 中。但这是一个 hack,如果我把它作为一个真实的答案,我可能会被枪杀。
  • 而且...我建议从一开始就学习 LINQ-to-XML (XDocument)。
  • @DavidStratton:如果 Xml 来自序列化的 DataTable(它可能是),这不是黑客攻击。
  • 我的错。我的意思是 Visual C#。

标签: c# parsing xmlreader


【解决方案1】:
var types = XDocument.Load("http://simon.ist.rit.edu:8080/Services/resources/ESD/OrgTypes/")
                .Descendants("type")
                .Select(t => (string)t)
                .ToList();

【讨论】:

  • 值始终是来自 Xml 文档的 string
  • @IAbstract 什么?我不明白你的评论。以上代码返回 14 个项目为 List&lt;string&gt;
  • 我是说演员 (string) 应该是不必要的。 :) ...但我也建议明确(因为我们的 OP 是 C# 的新手)并包括 t.Value
  • @IAbstract 不,这不是不必要的。和t.Value一样(t是XElement)
  • t.Value 不能产生空值,因为 Value 将返回一个空字符串。我删除了节点中的一个值并使用了t.Value——没有崩溃,没有空异常……只是一个空行。
【解决方案2】:

此方法确实不完整,值得为 C# 和 LINQ-to-XML 初学者做一些解释:

var types = XDocument.Load("http://simon.ist.rit.edu:8080/Services/resources/ESD/OrgTypes/")
                .Descendants("type")
                .Select(t => (string)t) // under the hood magic
                .ToList();

使用(string) 进行投射有点神奇,如果使用ToString(),则不会得到相同的结果。我来解释一下……我只是稍微修改了 XML:

<type attrib="bar" attrib2="boo" >Resource
    <foo a="1" a.2="A"/>
</type>
// note, I also removed the value of type immediately following Resource node

t 上的 (string) 在幕后作用于 t.Value。没有演员表,结果是:

<type>Physician</type>  
<type>Ambulance</type>  
<type>Fire Department</type>  
<type>Helicopter/Air Transport</type>  
<type>Home Care Agency</type>  
<type>Hospital</type>  
<type>Law Enforcement Agency</type>  
<type>Nursing Home</type>
<type attrib="bar" attrib2="boo">Resource  
                <foo a="1" a.2="A" /></type>  
<type></type>  
<type>Other</type>  
<type>Hospice</type>  
<type>School</type>  
<type>Emergency Shelter</type>  

使用(string)t

Physician
Ambulance
Fire Department
Helicopter/Air Transport
Home Care Agency
Hospital
Law Enforcement Agency
Nursing Home
Resource


Other
Hospice
School
Emergency Shelter

还有t.Value

Physician
Ambulance
Fire Department
Helicopter/Air Transport
Home Care Agency
Hospital
Law Enforcement Agency
Nursing Home
Resource


Other
Hospice
School
Emergency Shelter

最后,要表明 t.ToString()(string)t 不同:

<type>Physician</type>
<type>Ambulance</type>
<type>Fire Department</type>
<type>Helicopter/Air Transport</type>
<type>Home Care Agency</type>
<type>Hospital</type>
<type>Law Enforcement Agency</type>
<type>Nursing Home</type>
<type attrib="bar" attrib2="boo">Resource
                <foo a="1" a.2="A" /></type>
<type></type>
<type>Other</type>
<type>Hospice</type>
<type>School</type>
<type>Emergency Shelter</type>

所有这些都是为了重申一些鲜为人知的问题 LINQ-to-XML。

为了清晰和易于维护,我的建议如下:

var types = XDocument.Load("http://simon.ist.rit.edu:8080/Services/resources/ESD/OrgTypes/")
                .Descendants("type")
                .Select(t => t.Value) // be explicit about what you want
                .ToList();

您可以搜索IEnumerable 风格的任何elementdescendant

【讨论】:

  • 很好,如果问题是.Select(t =&gt; (string)t) 是什么意思
  • Yes ... and 它回答了 OP 的问题,同时解释了为什么另一个答案是隐式和错误的编码形式。我们应该提供完整的答案,对于新的 C# 用户,我期望更高的参与度。
  • it answers the OP's question while explaining .... 但是通过从其他答案中复制整个逻辑......所以解释别人的答案并不能成为答案。
  • 我添加了我自己的答案以及为什么我推荐它而不是另一个。因为我是以这种方式这样做的,所以我发了这篇文章 CW 以免收集任何代表。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-27
  • 2016-12-11
相关资源
最近更新 更多