【问题标题】:How do I access this XML structure using Linq in C#?如何在 C# 中使用 Linq 访问此 XML 结构?
【发布时间】:2011-08-24 11:04:00
【问题描述】:

我对这些东西很陌生,很难弄清楚如何正确访问我的数据。我所拥有的是这种形式的 XML 树:

<bpm:ResponseData
xmlns:bpm="http://rest.bpm.ibm.com/v1/data">
<status>200</status>    
<data
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srch="http://rest.bpm.ibm.com/v1/data/search"
xsi:type="srch:SearchDetails">  
    <data>  
        <item key="assignedToUser"/>    
        <item key="bpdName">    
            <value xmlns:ns5="http://www.w3.org/2001/XMLSchema" xsi:type="ns5:string">
                Some process name
            </value>
        </item>
        <item key="instanceDueDate">
            <value xmlns:ns5="http://www.w3.org/2001/XMLSchema" xsi:type="ns5:string">
                2011-09-06T12:35:48Z
            </value>
        </item>
        <item key="taskId">
            <value xmlns:ns5="http://www.w3.org/2001/XMLSchema" xsi:type="ns5:decimal">
                218
            </value>
        </item> 
        <item key="taskSubject">
            <value xmlns:ns5="http://www.w3.org/2001/XMLSchema" xsi:type="ns5:string">
                Task: Some process related task
            </value>
        </item>
    </data> 
    <data>  
        <item key="bpdName">
            <value xmlns:ns5="http://www.w3.org/2001/XMLSchema" xsi:type="ns5:string">
                Another process name
            </value>
        </item> 
        <item key="instanceStatus">
            <value xmlns:ns5="http://www.w3.org/2001/XMLSchema" xsi:type="ns5:string">
                Active
            </value>
        </item> 
        <item key="taskId"> 
            <value xmlns:ns5="http://www.w3.org/2001/XMLSchema" xsi:type="ns5:decimal">
                253
            </value>
        </item>
        <item key="taskSubject">    
            <value xmlns:ns5="http://www.w3.org/2001/XMLSchema" xsi:type="ns5:string">
                Task: Another process related task
            </value>
        </item>
    </data>
</data>
</bpm:ResponseData>

我需要从这些数据中准确提取两件事:taskSubject 和 taskId。最好以允许我迭代它们的方式。涉及 new{subject, id} 的东西会很好。

我不太确定如何处理事情任务...

var items = from feed in XMLDocument.Descendants("data").Descendants("data") select feed;

我得到了两个数据项。有什么方法可以进一步挖掘它们,返回具有特定“键”属性的后代的值?

问候, 迈克尔

编辑:

我认为这会起作用:

var items = from feed in XMLDocument.Descendants("data").Descendants("data") select 
    new{
        subject = from subjects in feed.Elements() where (subjects.Attribute("key").Value=="taskSubject") select subjects.Value,
        id = from subjects in feed.Elements() where (subjects.Attribute("key").Value == "taskId") select subjects.Value
        };

但这似乎很“肮脏”...

【问题讨论】:

标签: c# .net xml linq linq-to-xml


【解决方案1】:

这有点骇人听闻,但它应该可以工作(在 Mono 2.10.2 上测试):

var items = from data in document.Descendants("data")

            let taskId =
                data.Elements("item")
                .Where(i => (string)i.Attribute("key") == "taskId")
                .FirstOrDefault()

            where taskId != null

            let taskSubject = 
                data.Elements("item")
                .Where(i => (string)i.Attribute("key") == "taskSubject")
                .FirstOrDefault()

            where taskSubject != null

            select new {
                TaskId = taskId.Element("value").Value.Trim(),
                TaskSubject = taskSubject.Element("value").Value.Trim()
            };

【讨论】:

  • 确实如此。请注意,如果遇到没有key 属性的&lt;item&gt; 元素,您的解决方案将导致异常,因为您访问attributes Value` 属性,而不是让显式转换为字符串为您处理空情况。此外,在这种情况下,您的数据属性是 IEnumerable&lt;string&gt; 实例而不是 string 实例。
  • 是的,这就是我首先启动线程的原因。感谢您的帮助;)
  • 没问题。很高兴能帮上忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多