XmlSerializer.Serialize剥离<xml>标记</xml>

时间:2012-04-18 21:19:26

标签: c#

我正在尝试与支付处理器进行通信。当我使用XmlSerializer.Serialize时 在我的对象上我得到了

<?xml version=\"1.0\" encoding=\"utf-16\"?>
<txn xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
  <ssl_merchant_id>xxxxxx</ssl_merchant_id>
  <ssl_user_id>xxxxxx</ssl_user_id>
  <ssl_pin>xxxxxx</ssl_pin>
  <ssl_test_mode>True</ssl_test_mode>
  <ssl_transaction_type>ccavsonly</ssl_transaction_type>
  <ssl_card_number>4111111111111111</ssl_card_number>
  <ssl_exp_date>0612</ssl_exp_date>
  <ssl_avs_address>101 Main St.</ssl_avs_address>
  <ssl_avs_zip>90210</ssl_avs_zip>
</txn>

在使用该方法之前,我手动构建了XML以进行测试,这很有效:

<txn>
  <ssl_merchant_id>xxxxxx</ssl_merchant_id>
  <ssl_user_id>xxxxxx</ssl_user_id>
  <ssl_pin>xxxxxx</ssl_pin>
  <ssl_test_mode>True</ssl_test_mode>
  <ssl_transaction_type>ccavsonly</ssl_transaction_type>
  <ssl_card_number>4111111111111111</ssl_card_number>
  <ssl_exp_date>0612</ssl_exp_date>
  <ssl_avs_address>101 Main St.</ssl_avs_address>
  <ssl_avs_zip>90210</ssl_avs_zip>
</txn>

我如何从XML中删除<?xml version=\"1.0\" encoding=\"utf-16\"?>xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">,或者不让序列化程序生成它?

我的对象如下:

[XmlRoot(ElementName="txn")]
public class AvsTransmission
{
    [XmlElement]
    public string ssl_merchant_id { get; set; }
    [XmlElement]
    public string ssl_user_id { get; set; }
    [XmlElement]
    public string ssl_pin { get; set; }
    [XmlElement]
    public string ssl_test_mode { get; set; }
    [XmlElement]
    public string ssl_transaction_type { get; set; }
    [XmlElement]
    public string ssl_card_number { get; set; }
    [XmlElement]
    public string ssl_exp_date { get; set; }
    [XmlElement]
    public string ssl_avs_address { get; set; }
    [XmlElement]
    public string ssl_avs_zip { get; set; }
}

5 个答案:

答案 0 :(得分:31)

我的第一个答案只是问题的一半,你还必须删除已经提到过的声明。

以下是一个例子:

XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;

MemoryStream ms = new MemoryStream();
XmlWriter writer = XmlWriter.Create(ms, settings);

XmlSerializerNamespaces names = new XmlSerializerNamespaces();
names.Add("", "");

XmlSerializer cs = new XmlSerializer(typeof(Cat));

cs.Serialize(writer, new Cat { Lives = 9 }, names);

ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(ms);
var xml = sr.ReadToEnd();

字符串xml现在包含:

<cat><Lives>9</Lives></cat>

答案 1 :(得分:4)

第一个函数将对象转换为xml字符串,第二个转换对象并同时将xml写入文件。

    public static string Serialize<T>(T obj)
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.OmitXmlDeclaration = true;
        var writer = new StringWriter();
        XmlWriter xmlWriter = XmlWriter.Create(writer, settings);

        XmlSerializerNamespaces names = new XmlSerializerNamespaces();
        names.Add("", "");

        XmlSerializer serializer = new XmlSerializer(typeof(T));

        serializer.Serialize(xmlWriter, obj, names);
        var xml = writer.ToString();
        return xml;
    }

    public static void Serialize<T>(T obj, string filepath)
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.OmitXmlDeclaration = true;
        var writer = new StreamWriter(filepath);
        XmlWriter xmlWriter = XmlWriter.Create(writer, settings);

        XmlSerializerNamespaces names = new XmlSerializerNamespaces();
        names.Add("", "");

        XmlSerializer serializer = new XmlSerializer(typeof(T));

        serializer.Serialize(xmlWriter, obj, names);
    }

实际上我意识到代码重复了一遍。在这些方法中,只更改一个作为writer对象的东西,因此代码必须如下所示。通过这种方式,您只需更改编写器对象类型即可轻松使用代码。

    public static string Serialize<T>(T obj)
    {
        var writer = new StringWriter();
        Serialize<T>(obj,writer);
        var xml = writer.ToString();
        return xml;
    }

    public static void Serialize<T>(T obj, string filepath)
    {
        var writer = new StreamWriter(filepath);
        Serialize<T>(obj,writer);
    }

    public static void Serialize<T>(T obj, TextWriter writer)
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.OmitXmlDeclaration = true;
        XmlWriter xmlWriter = XmlWriter.Create(writer, settings);            
        XmlSerializerNamespaces names = new XmlSerializerNamespaces();
        names.Add("", "");            
        XmlSerializer serializer = new XmlSerializer(typeof(T));            
        serializer.Serialize(xmlWriter, obj, names);
    }

答案 2 :(得分:2)

如果您使用XmlWriter,则有一个设置可以省略声明 http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.omitxmldeclaration.aspx

我会做一个例子,但这可能足以让你前进。


编辑:这是一个在LinqPad中运行的示例

string test  = "test";
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
MemoryStream strm = new MemoryStream();
XmlWriter writer = XmlWriter.Create(strm, settings);
XmlSerializer serializer =  new XmlSerializer(typeof(string));
serializer.Serialize(writer, test);
strm.Position = 0;
StreamReader reader = new StreamReader(strm);
var x = reader.ReadToEnd();
x.Dump();
writer.Close();
reader.Close();
strm.Close();

输出

<string>test</string>

注释掉第3行,输出是

<?xml version="1.0" encoding="utf-8"?><string>test</string>

答案 3 :(得分:1)

非常类似于@ payo的orignal回答:

如果你想删除命名空间,你可能还想删除版本,为了节省你的搜索我已经添加了这个功能,所以下面的代码将同时执行这两个。

我还将它包装在一个通用方法中,因为我正在创建非常大的xml文件,这些文件太大而无法在内存中进行序列化,因此我将输出文件中断并将其序列化为较小的“块”:

public string XmlSerialize<T>(T entity) where T : class
{
    // removes version
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.OmitXmlDeclaration = true;

    XmlSerializer xsSubmit = new XmlSerializer(typeof(T));
    StringWriter sw = new StringWriter();
    using (XmlWriter writer = XmlWriter.Create(sw, settings))
    {
        // removes namespace
        var xmlns = new XmlSerializerNamespaces();
        xmlns.Add(string.Empty, string.Empty);

        xsSubmit.Serialize(writer, entity, xmlns);
        return sw.ToString(); // Your XML
    }
}

答案 4 :(得分:-1)

只是为了简化soln。删除UTF编码声明的原始问题 这是代码

                StringBuilder sb = new StringBuilder();
                var serializer = new System.Xml.Serialization.XmlSerializer(typeToSerialize.GetType());
                serializer.Serialize(System.Xml.XmlWriter.Create(sb, new System.Xml.XmlWriterSettings { OmitXmlDeclaration = true, Indent=true }), typeToSerialize);