如何在c#中向class元素添加名称空间前缀

时间:2016-06-02 16:13:34

标签: c# xml

 [XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/schema/SCRIPT")]
    public  class Identification
    {
        public string DEANumber { get; set; }
        public uint NPI { get; set; }
    }

<someprefix:Identification>
          <someprefix:DEANumber>FF1234567</DEANumber>
          <someprefix:NPI>1619967999</NPI>
  </someprefix:Identification>

如何为类元素指定名称空间前缀

1 个答案:

答案 0 :(得分:2)

使用XmlSerializerNamespaces

var id = new Identification()
{
    DEANumber = "qwe",
    NPI = 123,
};

var serializer = new XmlSerializer(typeof(Identification));
var xmlns = new XmlSerializerNamespaces();
xmlns.Add("someprefix", "http://www.example.org/schema/SCRIPT");

serializer.Serialize(Console.Out, id, xmlns);