本文介绍了创建Web服务的XML字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发送给需要包含XML,而我已经给了XSD字符串的Web服务请求。

I am sending a request to a web service which requires a string containing XML, of which I have been giving an XSD.

我已经跑了XSD.EXE和创建基于此一类,但我不能确定的创建XML字符串派出最好的方式,例如流,为XMLDocument或某种形式的序列化。

I've ran xsd.exe and created a class based on this but am unsure of the best way to create the xml string to send, for example a stream, XMLDocument or some form of serialization.

更新

我发现这个here

 public static string XmlSerialize(object o)
    {
        using (var stringWriter = new StringWriter())
        {
            var settings = new XmlWriterSettings
            {
                Encoding = Encoding.GetEncoding(1252),
                OmitXmlDeclaration = true
            };
            using (var writer = XmlWriter.Create(stringWriter, settings))
            {
                var xmlSerializer = new XmlSerializer(o.GetType());
                xmlSerializer.Serialize(writer, o);
            }
            return stringWriter.ToString();
        }
    }

这让我控制标记属性。

which lets me control the tag attribute.

感谢所有谁帮助。

推荐答案

我做几次什么是创建一个类/结构来保存在客户端程序中的数据和序列化数据为字符串。然后,我把web请求,并把它的XML字符串。这里是code我使用序列化一个对象到XML:

What I am doing on several occasions is creating a class/struct to hold the data on the client-side program and serializing the data as a string. Then I make the web request and send it that XML string. Here is the code I use to serialize an object to XML:

public static string SerializeToString(object o)
{
    string serialized = "";
    System.Text.StringBuilder sb = new System.Text.StringBuilder();

    //Serialize to memory stream
    System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(o.GetType());
    System.IO.TextWriter w = new System.IO.StringWriter(sb);
    ser.Serialize(w, o);
    w.Close();

    //Read to string
    serialized = sb.ToString();
    return serialized;
}

只要对象的所有内容都是可序列化,将任何对象序列化。

As long as all the contents of the object are serializable it will serialize any object.

这篇关于创建Web服务的XML字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:42