从 Web 服务传递 Xml
我有一个 Web 服务,它返回 MyData
类型的内容。
public class MyData
{
public string Name;
[XmlElement("item")]
public Object[] DataItems;
}
我已将 Object[]
用于 DataItems
,因为数组的类型可能有多种类型。我有两个不同的类,我可以使用此方法成功发送它们。见下文。
clientResults
是填充的数据集。
MyData returnResult = new MyData();
MyFirstClass[] resultData = new MyFirstClass[clientResults.Tables[0].Rows.Count];
resultData.MyFirstClassProperty1 = "Blah Blah";
resultData.MyFirstClassProperty2 = "Blah Blah";
returnResult.DataItems = resultData.
我可以轻松地将 MyFirstClass
更改为 MySecondClass
并设置其自己的属性,并且 Web 服务将正确序列化这两个类,每个人都很高兴!
但是现在我需要传递由 DataSet.GetXml()
函数返回的 XML。
当然,我所做的是
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(clientResults.GetXml());
resultData.DataItems = new XmlDocument[] { xdoc };
但这引发了异常
System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type System.Xml.XmlDocument may not be used in this context.
所以我想,好吧,让我们用 XmlNode 尝试一下。
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(clientResults.GetXml());
XmlNode xElement = xdoc.SelectSingleNode("/");
result.DataItems = new XmlNode[] { xElement };
仍然抛出相同的异常。可能出什么问题了?
如何通过 Web 服务正确传递 XML?
I have a web service which returns something of type MyData
.
public class MyData
{
public string Name;
[XmlElement("item")]
public Object[] DataItems;
}
I have used Object[]
for DataItems
because the type of array could be of several types. I have two different classes which I could successfully send using this method. See below.
clientResults
is the filled DataSet.
MyData returnResult = new MyData();
MyFirstClass[] resultData = new MyFirstClass[clientResults.Tables[0].Rows.Count];
resultData.MyFirstClassProperty1 = "Blah Blah";
resultData.MyFirstClassProperty2 = "Blah Blah";
returnResult.DataItems = resultData.
I could easily change MyFirstClass
to MySecondClass
and set its own properties and the web service would properly serialize both the classes and every one was happy!
However now there is a need where I have to pass an XML returned by the DataSet.GetXml()
function.
Naturally, what I did was
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(clientResults.GetXml());
resultData.DataItems = new XmlDocument[] { xdoc };
But this is throwing an exception
System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type System.Xml.XmlDocument may not be used in this context.
So what I thought, ok lets try it with XmlNode.
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(clientResults.GetXml());
XmlNode xElement = xdoc.SelectSingleNode("/");
result.DataItems = new XmlNode[] { xElement };
Still its throwing the SAME exception. What could be wrong?
How do I properly pass an XML through a web service?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案很简单。我所要做的就是创建一个其他类将从其继承的父类。
我将
Object[]
设为BaseData[]
。然后我使用 XPath 选择节点并分配它。
我还必须将
XmlInclude(typeof(XmlData))
添加到 Web 服务方法签名中。而且效果非常好!
The answer was pretty easy. All I had to do was create a parent class which other classes were going to inherit from.
And I made the
Object[]
to aBaseData[]
.And then I selected the node using XPath and assigned it.
I also had to put a
XmlInclude(typeof(XmlData))
to the web service method signature.And it was working perfectly!