簡體   English   中英

LINQ查詢XML C#

[英]LINQ query for XML c#

我有一個簡單的循環來查找子元素:

XDocument xDoc = XDocument.Load(@"bpmn.xml");
//Run query
foreach (XElement level1Element in xelement.Elements("process"))
{
    System.Diagnostics.Debug.WriteLine(level1Element.Attribute("id").Value);

    foreach (XElement level2Element in level1Element.Elements("task"))
    {
        System.Diagnostics.Debug.WriteLine(
            "  " + level2Element.Attribute("name").Value);
    }
}

為什么我沒有得到任何輸出?

輸入XML:

<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:tns="http://sourceforge.net/bpmn/definitions/_1384266732154" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:yaoqiang="http://bpmn.sourceforge.net" exporter="Yaoqiang BPMN Editor" exporterVersion="2.2.18 (GPLv3, Non-Commercial)" expressionLanguage="http://www.w3.org/1999/XPath" id="_1384266732154" name="" targetNamespace="http://sourceforge.net/bpmn/definitions/_1384266732154" typeLanguage="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL http://bpmn.sourceforge.net/schemas/BPMN20.xsd">   <collaboration id="COLLABORATION_1" isClosed="false">
    <participant id="_2" name="CEO" processRef="PROCESS_1">
      <participantMultiplicity maximum="1" minimum="0"/>
    </participant>
    <participant id="_3" name="Manager" processRef="PROCESS_2">
      <participantMultiplicity maximum="1" minimum="0"/>
    </participant>
    <participant id="_4" name="project Manager" processRef="PROCESS_3">
      <participantMultiplicity maximum="1" minimum="0"/>
    </participant>
    <participant id="_5" name="HR" processRef="PROCESS_4">
      <participantMultiplicity maximum="1" minimum="0"/>
    </participant>
    <participant id="_6" name="Employee" processRef="PROCESS_5">
      <participantMultiplicity maximum="1" minimum="0"/>
    </participant>   </collaboration>   <process id="PROCESS_1" isClosed="false" isExecutable="true" processType="None">
    <startEvent id="_8" isInterrupting="true" name="Start Event" parallelMultiple="false">
      <outgoing>_12</outgoing>
    </startEvent>
    <task completionQuantity="1" id="_9" isForCompensation="false" name="Task" startQuantity="1">
      <incoming>_12</incoming>
      <outgoing>_13</outgoing>
    </task>
    <sendTask completionQuantity="1" id="_10" implementation="##WebService" isForCompensation="false" name="Send Task" startQuantity="1">
      <incoming>_13</incoming>
      <outgoing>_14</outgoing>
    </sendTask>
    <task completionQuantity="1" id="_11" isForCompensation="false" name="Task" startQuantity="1">
      <incoming>_14</incoming>
    </task>
    <sequenceFlow id="_12" sourceRef="_8" targetRef="_9"/>
    <sequenceFlow id="_13" sourceRef="_9" targetRef="_10"/>
    <sequenceFlow id="_14" sourceRef="_10" targetRef="_11"/>   </process>   <process id="PROCESS_2" isClosed="false" isExecutable="true" processType="None">
    <task completionQuantity="1" id="_17" isForCompensation="false" name="Task" startQuantity="1">
      <outgoing>_21</outgoing>
    </task>
    <task completionQuantity="1" id="_19" isForCompensation="false" name="Task" startQuantity="1">
      <incoming>_21</incoming>
      <outgoing>_23</outgoing>
      <outgoing>_25</outgoing>
    </task>
    <sequenceFlow id="_21" sourceRef="_17" targetRef="_19"/>
    <receiveTask completionQuantity="1" id="_22" implementation="##WebService" instantiate="false" isForCompensation="false" name="Receive Task" startQuantity="1">
      <incoming>_23</incoming>
    </receiveTask>
    <sequenceFlow id="_23" name="go on" sourceRef="_19" targetRef="_22"/>
    <task completionQuantity="1" id="_24" isForCompensation="false" name="Task" startQuantity="1">
      <incoming>_25</incoming>
    </task>
    <sequenceFlow id="_25" sourceRef="_19" targetRef="_24"/>   </process> </definitions>

您需要從根元素中進行選擇。 嘗試更改:

xelement.Elements("process"))

至:

xDoc.Elements("definitions").Elements("process")

您的.xml文件提供了命名空間,但仍應在代碼中使用它。 請嘗試以下方法:

 XDocument xDoc = XDocument.Load(@"xmlfile1.xml");
  //Run query

  XNamespace ns = "http://www.omg.org/spec/BPMN/20100524/MODEL";
  foreach (XElement level1Element in xDoc.Descendants(ns + "process"))
  {
    System.Diagnostics.Debug.WriteLine(level1Element.Attribute("id").Value);

    foreach (XElement level2Element in level1Element.Elements(ns + "task"))
    {
      System.Diagnostics.Debug.WriteLine(
          "  " + level2Element.Attribute("name").Value);
    }
  }

另外,在您的情況下,請使用Descendants()方法代替Elements() 更多信息http://msdn.microsoft.com/zh-cn/library/system.xml.linq.xname(v=vs.110).aspx

  1. 您需要先通過xDoc.Root獲取根元素(如果代碼中的xelementxDoc
  2. (這里的主要問題)您的XML具有自定義名稱空間,在這種情況下,您需要在調用Elements()時指定它。

放在一起:

    XDocument xDoc = XDocument.Load(@"bpmn.xml");
    var ns = XNamespace.Get("http://www.omg.org/spec/BPMN/20100524/MODEL");
    //Run query
    foreach(XElement level1Element in xDoc.Root.Elements(ns + "process"))
    {
        System.Diagnostics.Debug.WriteLine(level1Element.Attribute("id").Value);

        foreach(XElement level2Element in level1Element.Elements(ns + "task"))
        {
            System.Diagnostics.Debug.WriteLine("  " + level2Element.Attribute("name").Value);
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM