简体   繁体   English

使用LINQ to XML从WSDL解析xsd

[英]parsing xsd from WSDL using LINQ to XML

I am trying to build a dictionary using an XSD file which I get from WSDL definition using LINQ to XML. 我正在尝试使用XSD文件构建字典,该文件是使用LINQ to XML从WSDL定义中获得的。

The nodes which I am trying to parse look something like this 我试图解析的节点看起来像这样

<xsd:element maxOccurs="1" minOccurs="0" name="active" type="xsd:boolean"/>
<xsd:element maxOccurs="1" minOccurs="0" name="activity_due" type="xsd:string"/>
<xsd:element maxOccurs="1" minOccurs="0" name="additional_assignee_list" type="xsd:string"/>
<xsd:element maxOccurs="1" minOccurs="0" name="approval" type="xsd:string"/>
<xsd:element maxOccurs="1" minOccurs="0" name="approval_history" type="xsd:string"/>
<xsd:element maxOccurs="1" minOccurs="0" name="approval_set" type="xsd:string"/>
<xsd:element maxOccurs="1" minOccurs="0" name="assigned_to" type="xsd:string"/>
<xsd:element maxOccurs="1" minOccurs="0" name="assignment_group" type="xsd:string"/>

The link to the XML file is: https://dl.dropboxusercontent.com/u/97162408/incident.xml XML文件的链接是: https : //dl.dropboxusercontent.com/u/97162408/incident.xml

I am only worried about "getKeys". 我只担心“ getKeys”。

Basically want to build a dictionary which will give me a key-value pair for "name" and "type" from the above sample node list. 基本上想构建一个字典,该字典将为我提供上述示例节点列表中“名称”和“类型”的键值对。

I have got to a point where I can get to the Node list using the code 我已经到了可以使用代码访问“节点”列表的地步

 XNamespace ns = XNamespace.Get("http://www.w3.org/2001/XMLSchema");

 XDocument xd = XDocument.Load(url);

 var result = (from elements in xd.Descendants(ns + "element") where elements.Attribute("name").Value.Equals("getKeys") 

                          select elements.Descendants(ns + "sequence")
                          );

Now I wanted to build a dictionary in a single function call without writing another routine to parse through the result list using LINQ to XML. 现在,我想在一个函数调用中构建一个字典,而无需编写另一个例程来使用LINQ to XML解析结果列表。 Any hints, code samples would be really helpful!! 任何提示,代码示例将非常有帮助!

ToDictionary is your friend here. ToDictionary是您的朋友在这里。 You can do it all in one statement: 您可以在一条语句中完成所有操作:

var query = xd
    .Descendants(ns + "element")
    .Single(element => (string) element.Attribute("name") == "getKeys")
    .Element(ns + "complexType")
    .Element(ns + "sequence")
    .Elements(ns + "element")
    .ToDictionary(x => (string) x.Attribute("name"),
                  x => (string) x.Attribute("type"));

Basically the first three lines find the only element with a name of getKeys , the next two three lines select the xsd:element parts under it (you could just use Descendants(ns + "element") if you wanted), and the final call transforms a sequence of elements into a Dictionary<string, string> . 基本上,前三行找到唯一一个名为getKeys元素,后三行选择其下的xsd:element部分(如果需要,可以使用Descendants(ns + "element") ),最后调用将元素序列转换为Dictionary<string, string>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM