将序列化对象与现有 xml 文档合并并返回它?

发布于 2024-09-08 13:40:13 字数 1285 浏览 2 评论 0原文

我试图找出将序列化对象与现有 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 技术交流群。

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

发布评论

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

评论(3

倾听心声的旋律 2024-09-15 13:40:14

您可以序列化没有名称空间和根的对象,然后合并它。

不使用命名空间进行序列化

You can serialize the object without the namespace and the root and then merge it.

Serializing without the namespace

影子的影子 2024-09-15 13:40:14

对于我的情况,这有效:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

public WebService()
{

    //Uncomment the following line if using designed components 
    //InitializeComponent(); 
}

[WebMethod]
public XmlDocument 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>

    // Serialization

    string serializedXML = SerializeObject(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
    XmlDocument xdoc = new XmlDocument();
    xdoc.Load(TemplatePath);
    XmlNodeList datagoeshere = xdoc.GetElementsByTagName("DATA-GOES-HERE");
    if (datagoeshere != null && datagoeshere.Count > 0)
        datagoeshere[0].InnerXml = serializedXML;


    return xdoc;
}
/// <summary>
/// To convert a Byte Array of Unicode values (UTF-8 encoded) to a complete String.
/// </summary>
/// <param name="characters">Unicode Byte Array to be converted to String</param>
/// <returns>String converted from Unicode Byte Array</returns>
private String UTF8ByteArrayToString(Byte[] characters)
{
    UTF8Encoding encoding = new UTF8Encoding();
    String constructedString = encoding.GetString(characters);
    return (constructedString);
}

/// <summary>
/// Converts the String to UTF8 Byte array and is used in De serialization
/// </summary>
/// <param name="pXmlString"></param>
/// <returns></returns>
private Byte[] StringToUTF8ByteArray(String pXmlString)
{
    UTF8Encoding encoding = new UTF8Encoding();
    Byte[] byteArray = encoding.GetBytes(pXmlString);
    return byteArray;
}

/// <summary>
/// Method to convert a custom Object to XML string
/// </summary>
/// <param name="pObject">Object that is to be serialized to XML</param>
/// <returns>XML string</returns>
public String SerializeObject(Object pObject)
{
    try
    {
        //Create our own namespaces for the output
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

        //Add an empty namespace and empty value
        ns.Add("", "");


        String XmlizedString = null;
        MemoryStream memoryStream = new MemoryStream();
        XmlSerializer xs = new XmlSerializer(typeof(Apples));
        XmlTextWriter xmlTextWriter = new XmlTextWriterFormattedNoDeclaration(memoryStream, Encoding.UTF8);

        xs.Serialize(xmlTextWriter, pObject, ns);
        memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
        XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
        return XmlizedString.Trim();
    }
    catch (Exception e)
    {
        System.Console.WriteLine(e);
        return null;
    }
}

public class XmlTextWriterFormattedNoDeclaration : System.Xml.XmlTextWriter
{
    public XmlTextWriterFormattedNoDeclaration(Stream w, Encoding encoding)
        : base(w, encoding)
    {
        Formatting = System.Xml.Formatting.Indented;
    }

    public override void WriteStartDocument() { } // suppress
} }

网络服务的响应是:

<?xml version="1.0" encoding="utf-8"?>
<applereport>
  <moretags>
    <july>
      <report>
        <DATA-GOES-HERE>
          <Apples>
  <Name>Smtih</Name>
  <Size>11</Size>

  <Weight>111</Weight>
  <Color>Red</Color>
</Apples>
        </DATA-GOES-HERE>
      </report>
    </july>
  </moretags>
</applereport>

for my situation this worked:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

public WebService()
{

    //Uncomment the following line if using designed components 
    //InitializeComponent(); 
}

[WebMethod]
public XmlDocument 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>

    // Serialization

    string serializedXML = SerializeObject(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
    XmlDocument xdoc = new XmlDocument();
    xdoc.Load(TemplatePath);
    XmlNodeList datagoeshere = xdoc.GetElementsByTagName("DATA-GOES-HERE");
    if (datagoeshere != null && datagoeshere.Count > 0)
        datagoeshere[0].InnerXml = serializedXML;


    return xdoc;
}
/// <summary>
/// To convert a Byte Array of Unicode values (UTF-8 encoded) to a complete String.
/// </summary>
/// <param name="characters">Unicode Byte Array to be converted to String</param>
/// <returns>String converted from Unicode Byte Array</returns>
private String UTF8ByteArrayToString(Byte[] characters)
{
    UTF8Encoding encoding = new UTF8Encoding();
    String constructedString = encoding.GetString(characters);
    return (constructedString);
}

/// <summary>
/// Converts the String to UTF8 Byte array and is used in De serialization
/// </summary>
/// <param name="pXmlString"></param>
/// <returns></returns>
private Byte[] StringToUTF8ByteArray(String pXmlString)
{
    UTF8Encoding encoding = new UTF8Encoding();
    Byte[] byteArray = encoding.GetBytes(pXmlString);
    return byteArray;
}

/// <summary>
/// Method to convert a custom Object to XML string
/// </summary>
/// <param name="pObject">Object that is to be serialized to XML</param>
/// <returns>XML string</returns>
public String SerializeObject(Object pObject)
{
    try
    {
        //Create our own namespaces for the output
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

        //Add an empty namespace and empty value
        ns.Add("", "");


        String XmlizedString = null;
        MemoryStream memoryStream = new MemoryStream();
        XmlSerializer xs = new XmlSerializer(typeof(Apples));
        XmlTextWriter xmlTextWriter = new XmlTextWriterFormattedNoDeclaration(memoryStream, Encoding.UTF8);

        xs.Serialize(xmlTextWriter, pObject, ns);
        memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
        XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
        return XmlizedString.Trim();
    }
    catch (Exception e)
    {
        System.Console.WriteLine(e);
        return null;
    }
}

public class XmlTextWriterFormattedNoDeclaration : System.Xml.XmlTextWriter
{
    public XmlTextWriterFormattedNoDeclaration(Stream w, Encoding encoding)
        : base(w, encoding)
    {
        Formatting = System.Xml.Formatting.Indented;
    }

    public override void WriteStartDocument() { } // suppress
} }

and the response from the webserivce is:

<?xml version="1.0" encoding="utf-8"?>
<applereport>
  <moretags>
    <july>
      <report>
        <DATA-GOES-HERE>
          <Apples>
  <Name>Smtih</Name>
  <Size>11</Size>

  <Weight>111</Weight>
  <Color>Red</Color>
</Apples>
        </DATA-GOES-HERE>
      </report>
    </july>
  </moretags>
</applereport>
染火枫林 2024-09-15 13:40:14

我现在没有时间查看完整示例,但请查看 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 the AppendChild method to produce an XmlWriter. Use that XmlWriter as the parameter to XmlSerializer.Serialize.

You can then return the entire XmlDocument.DocumentElement from the web service using the XmlElement type as the return type of the WebMethod.

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