将序列化对象与现有 xml 文档合并并返回它?
我试图找出将序列化对象与现有 xml 文档结合起来并将其作为 Web 服务返回。
提前感谢您的帮助。
示例代码:
[WebMethod]
public string GetApple()
{
Apples apples = Report.GetReport();
// this returns:
// <Apples xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
// <Name>Smtih</Name>
// <Size>11</Size>
// <Weight>111</Weight>
// <Color>Red</Color>
// </Apples>
string TemplatePath = Server.MapPath("~/Template.xml");
// this is:
// <?xml version="1.0" encoding="utf-8" ?>
// <applereport>
// <moretags>
// <july>
// <report>
// <DATA-GOES-HERE></DATA-GOES-HERE>
// </report>
// </july>
// </moretags>
// </applereport>
// Read TemplatePath into a memory stream
// find the node: <DATA-GOES-HERE></DATA-GOES-HERE>
//
// put serialixed output of Report.GetReport() where <DATA-GOES-HERE></DATA-GOES-HERE> is
return FullReport;
}
I'm trying to figure out to to combine a serialized object with an existing xml doc and return it as a webservice.
Thanks ahead for you help.
Sample code:
[WebMethod]
public string GetApple()
{
Apples apples = Report.GetReport();
// this returns:
// <Apples xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
// <Name>Smtih</Name>
// <Size>11</Size>
// <Weight>111</Weight>
// <Color>Red</Color>
// </Apples>
string TemplatePath = Server.MapPath("~/Template.xml");
// this is:
// <?xml version="1.0" encoding="utf-8" ?>
// <applereport>
// <moretags>
// <july>
// <report>
// <DATA-GOES-HERE></DATA-GOES-HERE>
// </report>
// </july>
// </moretags>
// </applereport>
// Read TemplatePath into a memory stream
// find the node: <DATA-GOES-HERE></DATA-GOES-HERE>
//
// put serialixed output of Report.GetReport() where <DATA-GOES-HERE></DATA-GOES-HERE> is
return FullReport;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以序列化没有名称空间和根的对象,然后合并它。
不使用命名空间进行序列化
You can serialize the object without the namespace and the root and then merge it.
Serializing without the namespace
对于我的情况,这有效:
网络服务的响应是:
for my situation this worked:
and the response from the webserivce is:
我现在没有时间查看完整示例,但请查看 XPathNavigator .AppendChild 方法。
这个想法是获取一个
XPathNavigator
实例,该实例指向文档中要添加数据的位置,然后使用AppendChild
方法生成一个XmlWriter
。使用该XmlWriter
作为XmlSerializer.Serialize
的参数。然后,您可以使用
XmlElement
类型作为WebMethod
的返回类型,从 Web 服务返回整个XmlDocument.DocumentElement
。I don't have time right now for a full example, but look at the XPathNavigator.AppendChild method.
The idea is to get an
XPathNavigator
instance pointing in your document to where you want to add data, then use theAppendChild
method to produce anXmlWriter
. Use thatXmlWriter
as the parameter toXmlSerializer.Serialize
.You can then return the entire
XmlDocument.DocumentElement
from the web service using theXmlElement
type as the return type of theWebMethod
.