如何在c#中添加自定义标签

时间:2017-03-24 03:36:17

标签: c# html xml

我有InvMgntType类,序列化类我试图在每个标记前面添加自定义标签。我尝试在序列化时传递XmlRootAttribute,但输出xml有一些垃圾值。请建议我如何解决这个问题。

enter image description here

sample code:
using System.Xml.Serialization;


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://StoreManagement/1.0")]
public partial class PSystemType
{
    private PSystemNameType participatingSystemNameField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public PSystemNameType participatingSystemName
    {
        get
        {
            return this.participatingSystemNameField;
        }
        set
        {
            this.participatingSystemNameField = value;
        }
    }

}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://StoreManagement/1.0")]
public enum PSystemNameType
{
    /// <remarks/>
    Store1,
    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("Store2")]
    Store2,
    /// <remarks/>
    Store3,
}


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace= "http://StoreManagement/1.0")]
[System.Xml.Serialization.XmlRootAttribute("InvMgntType", Namespace="http://StoreManagement/1.0", IsNullable=false)]
public partial class InvMgntType {

    private PSystemType originatingSystemField;

    /// <remarks/>
    public PSystemType OriginatingSystem {
        get {
            return this.originatingSystemField;
        }
        set {
            this.originatingSystemField = value;
        }
    }
}

[HttpGet]
[Route("InvTest")]
public IActionResult InvTest()
{
    var xr = new XmlRootAttribute();
    xr.ElementName = "sm:InvMgntType";
    var customRoot = xr as XmlRootAttribute;

    var obj = new InvMgntType();
    obj.OriginatingSystem = new PSystemType();
    obj.OriginatingSystem.participatingSystemName = PSystemNameType.Store1;
    var xmlString = Utility.Serialize(obj);
    return (new ObjectResult(xmlString) { StatusCode = 200 });
}
public static string Serialize<T>(this T value)
{
    if (value == null)
    {
        return string.Empty;
    }
    try
    {
        var xmlserializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
        var stringWriter = new System.IO.StringWriter();
        using (var writer = System.Xml.XmlWriter.Create(stringWriter))
        {
            xmlserializer.Serialize(writer, value);
            return stringWriter.ToString();
        }
    }
    catch (Exception ex)
    {
        throw new Exception("An error occurred", ex);
    }
}

0 个答案:

没有答案