我有一个XML文档(描述wsdl服务的接口):
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<s:element name="GetDummyType">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="param1" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="GetDummyTypeResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="GetDummyTypeResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="SimplestWebService">
<s:complexType />
</s:element>
<s:element name="SimplestWebServiceResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="SimplestWebServiceResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="SignInComp">
<s:complexType />
</s:element>
<s:element name="SignInCompResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="SignInCompResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element> ...我需要对上面的xml执行两个操作:
到目前为止,我只将这个文档解析为一个XmlDocument:
XmlDocument doc = new XmlDocument();
doc.LoadXml(result.ToString()); (我知道这并不多)
我只是不知道XML是如何映射到可以使用linq的东西上的。
你怎么做到的?
谢谢。
发布于 2010-09-29 12:43:54
您需要确保在查询中使用正确的XML名称空间。另外,对于LINQ,请使用XDocument,而不是XmlDocument,它来自旧的System.Xml。
到目前为止,这就是我想出来的:
XDocument doc = XDocument.Parse(xml);
XNamespace wsdl = "http://schemas.xmlsoap.org/wsdl/";
XNamespace s = "http://www.w3.org/2001/XMLSchema";
var schema = doc.Root
.Element(wsdl + "types")
.Element(s + "schema");
var elements = schema.Elements(s + "element");
Func<XElement, string> getName = (el) => el.Attribute("name").Value;
// these are all method names
var names = from el in elements
let name = getName(el)
where !name.EndsWith("Response")
select name;
string methodName = "GetDummyType";
var method = elements
.Single(el => getName(el) == methodName);
// these are all parameters for a given method
var parameters = from par in method.Descendants(s + "element")
select getName(par);我已经测试过这段代码,它可以在你的数据上工作。
然而,我并不完全是,这是最简单的解决方案,所以我欢迎任何关于缩短代码的建议。
最好的
丹
发布于 2010-09-29 12:06:25
对于#1,使用程序集System.Linq.Xml,您可以这样做:
List<string> names = new List<string>();
XDocument doc = Xdocument.Parse(result.ToString());
foreach (XElement element in doc.Elements("wsdl:types").First().Elements("s:schema").First().Elements("s:element"))
{
names.Add(element.Attributes("name").First().Value);
}它没有经过测试,所以您可能需要对代码进行一点调优;)
顺便说一下,您可以找到更多关于与System.Xml.Linq有关的的信息。
发布于 2010-09-29 12:44:40
使用linq到xml,您可以这样做-
XDocument doc = XDocument.Parse( xmlstring );
var methods = from methods in doc.Descendants( "s:element" )
where !methods.Attribute("name").Value.EndsWith("Resopnse")
select methods;
var methodNames = ( from method in methods
select method.Attribute( "name" ).Value ).ToList( );
var paramList = from type in methods.Descendants( "s:complexType" )
from param in type.Descendants("s:sequence")
where type.HasElements && type.Parent.Attribute("name").Value == somemethodname
select new { Name = param.Attribute( "name" ).Value };添加对system.core和system.xml.linq的引用
https://stackoverflow.com/questions/3821262
复制相似问题