从 Web 服务传递 Xml

发布于 2024-11-14 18:53:30 字数 1487 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情丝乱 2024-11-21 18:53:30

答案很简单。我所要做的就是创建一个其他类将从其继承的父类。

public class BaseData
{

}

public class XmlData : BaseData
{
   public XmlNode xml;
}

我将 Object[] 设为 BaseData[]

public class MyData
 {
        public string Name;
        [XmlElement("item")]
        public BaseData[] DataItems;
 }

然后我使用 XPath 选择节点并分配它。

XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(clientResults.GetXml());
XmlNode xElement = xdoc.SelectSingleNode("/");

XmlData[] xmlData = new XmlData[1];
xmlData[0] = new XmlData();
xmlData[0].xml = xElement;

result.DataItems = xmlData;

我还必须将 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.

public class BaseData
{

}

public class XmlData : BaseData
{
   public XmlNode xml;
}

And I made the Object[] to a BaseData[].

public class MyData
 {
        public string Name;
        [XmlElement("item")]
        public BaseData[] DataItems;
 }

And then I selected the node using XPath and assigned it.

XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(clientResults.GetXml());
XmlNode xElement = xdoc.SelectSingleNode("/");

XmlData[] xmlData = new XmlData[1];
xmlData[0] = new XmlData();
xmlData[0].xml = xElement;

result.DataItems = xmlData;

I also had to put a XmlInclude(typeof(XmlData)) to the web service method signature.

And it was working perfectly!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文