无法正确地将类实例序列化为XML

时间:2018-06-12 12:58:17

标签: .net xmlserializer

这是课程定义:

[Serializable()]
    public class BaseClass
    {
        public SYSMessage SYSMessage { get; set; }
    }

    [Serializable()]
    public class SYSMessage
    {
        public string messageGuid { get; set; }
    }

这就是我尝试获取serml xml string的方法;

public void xmlSerializer()
    {
        XmlSerializer xsSubmit = new XmlSerializer(typeof(BaseClass));
        var subReq = new BaseClass();
        var result = "";

        using (var sww = new StringWriter())
        {
            using (XmlWriter writer = XmlWriter.Create(sww))
            {
                xsSubmit.Serialize(writer, subReq);
                result = sww.ToString();
            }
        }
    }

这是结果;

<?xml version="1.0" encoding="utf-16"?>
<BaseClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <SYSMessage>
        <messageGuid>Test Value</messageGuid>
    </SYSMessage>
</BaseClass>

但这就是我需要它的方式;

...
    <SYSMessage>
        <messageGuid value="Test Value" />
   </SYSMessage>
    ...

0 个答案:

没有答案