接受SOAP数据作为原始XML



我正在尝试使用System.ServiceModel命名空间编写.NETIISweb服务。我的服务将从传入数据包中读取startTag节点作为RAW XML,而不是一个取消序列化的.NET类,但我似乎无法做到这一点

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:tmk="http://schemas.datacontract.org/2004/07/TMK.Catalog">
<soapenv:Header/>
<soapenv:Body>
<tem:Catalog>
<tem:document>
<tmk:CHRMAS03>
<startTag>
<myTag>
<answer>Hello world!</answer>
</myTag>
</startTag>
</tmk:CHRMAS03>
</tem:document>
</tem:Catalog>
</soapenv:Body>
</soapenv:Envelope>

这是我的网络服务:

using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Xml.XPath;
[ServiceContract]
public interface IContract
{
[OperationContract]
string Catalog(Point document);
}
[DataContract]
public class Point
{
[DataMember]
public XPathDocument CHRMAS03;
public Point (XPathDocument document)
{
this.CHRMAS03= document;
}
}
public class Service1 : IContract
{
public string Catalog (Point document)
{
if(document.CHRMAS03!=null) 
{
try
{
XPathNavigator navigator = document.CHRMAS03.CreateNavigator();
XPathNodeIterator iterator1 =  navigator.Select("startTag/myTag/answer");
if(iterator1==null) {return "crash"; }
int a = iterator1.Count; // Exception!
return "success"; 
}
catch(Exception e)
{
return e.Message + e.StackTrace;
}
}
return "fail";
}

当我将上面指定的数据包发送到我的web服务时,这一行:

int a = iterator1.Count

抛出异常:

Object reference not set to an instance of an object.
at MS.Internal.Xml.Cache.XPathDocumentNavigator.get_NameTable()
at MS.Internal.Xml.Cache.XPathDocumentElementChildIterator..ctor(XPathDocumentNavigator parent, String name, String namespaceURI)
at MS.Internal.Xml.Cache.XPathDocumentNavigator.SelectChildren(String name, String namespaceURI)
at MS.Internal.Xml.XPath.ChildrenQuery.Advance()
at MS.Internal.Xml.XPath.ChildrenQuery.Advance()
at MS.Internal.Xml.XPath.ChildrenQuery.Advance()
at MS.Internal.Xml.XPath.Query.MoveNext()
at MS.Internal.Xml.XPath.Query.get_Count()
at TMK.Catalog.Service1.Catalog(Point document)

我应该如何重新定义CHRMAS03字段,以便它将startTag节点的原始XML内容作为一个字符串,我可以手动解析该字符串,也可以作为任何标准.NET类的工作实例来读取XML,如XmlReaderXmlNode

"startTeg/myTeg/answer";应该是";startTag/myTag/answer"?

我的XPath查询:

startTag/myTag/answer

由于XML是用标记之间的空白元素解析的,因此无法工作。我切换到XMLDocument并修改代码以跳过它们:

XmlNode root = doc.DocumentElement;
foreach (XmlNode idoc in root.ChildNodes)
{
if (idoc.Name == "#whitespace") continue;
/* ... processing ... */
}

我认为这个空白元素导致了XPathNodeIterator.Count抛出。

相关内容

  • 没有找到相关文章

最新更新