如何在C#中使用Linq访问此XML结构?

时间:2011-08-24 11:04:01

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

我对这些东西很陌生,并且很难弄清楚如何正确访问我的数据。我所拥有的是这种形式的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。优选地,以允许我迭代它们的方式。涉及新{subject,id}的东西会很好。

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

使用

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

我得到了两个数据项。有没有办法进一步深入研究它们,并使用特定的“key”属性返回后代的值?

此致 迈克尔

编辑:

我认为这样可行:

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
        };

但这看起来很“脏”......

1 个答案:

答案 0 :(得分:2)

这有点hackish,但它应该工作(在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()
            };