C#XML序列化 - 命名空间引用的Schema问题

时间:2016-12-12 14:25:37

标签: c# xml-serialization

我将类序列化为特定的XML格式。以下是班级结构&代码我用它来序列化它。已删除的文件往往会遗漏元素名称“地址”。

我有3个类StudentInfo,它有两个属性类型为“Student”& “地址”

[Serializable]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRoot("StudentInfo", Namespace = "http://TestBizTalkMap.Student")]
public class StudentInfo
{
     [System.Xml.Serialization.XmlElement("", Namespace = "")]
     public Student Student { get; set; }

     [System.Xml.Serialization.XmlElement("ns1", Namespace = "ns1:http://TestBizTalkMap.Address")]

      public Address Address { get; set; }
}

[Serializable]
public class Student
{
    public string EnrollNo { get; set; }
    public string Name { get; set; }
    public string BTSReceivedOn { get; set; }
}

[Serializable]
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlRoot("Address", Namespace = "http://TestBizTalkMap.Address")]
public class Address
{
    [XmlElement("",Namespace="")]
    public string City { get; set; }
    [XmlElement("", Namespace = "")]
    public string State { get; set; }
}

代码用于序列化它:

public XmlDocument GetXMLSchema<T>(T type, string schemeNameSpace)
{
   XmlDocument xmlDoc = new XmlDocument();
   XmlSerializer xmlSerializer = new XmlSerializer(type.GetType());
   try
     {
          using (MemoryStream xmlStream = new MemoryStream())
          {
              XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
              ns.Add("ns0", schemeNameSpace);
              xmlSerializer.Serialize(xmlStream, type, ns);
              xmlStream.Position = 0;
              xmlDoc.Load(xmlStream);
              if (xmlDoc.FirstChild.NodeType == XmlNodeType.XmlDeclaration)
              {
                    xmlDoc.RemoveChild(xmlDoc.FirstChild);
              }
           }
     }
     catch (Exception ex)
     {
          throw ex;
     }
            return xmlDoc;
     }

将其序列化为

<ns0:StudentInfo xmlns:ns0="http://TestBizTalkMap.Student">
<Student>
<EnrollNo>EnrollNl</EnrollNo>
<Name>Name</Name>
<BTSReceivedOn>BTSReceivedOn</BTSReceivedOn>
</Student>

<ns1  xmlns="http://TestBizTalkMap.Address">
<City>City</City>
<State>State</State>
</ns1>
</ns0:StudentInfo

而我希望它被序列化为

<ns0:StudentInfo xmlns:ns0="http://TestBizTalkMap.Student">
  <Student>
    <EnrollNo>EnrollNo_0</EnrollNo>
    <Name>Name_0</Name>
    <BTSReceivedOn>BTSReceivedOn_0</BTSReceivedOn>
  </Student>

 <ns1:Adress xmlns:ns1="http://TestBizTalkMap.Address">
    <City>City_0</City>
    <State>State_0</State>
  </ns1:Adress>
</ns0:StudentInfo>

任何帮助?

由于

1 个答案:

答案 0 :(得分:2)

您似乎在名称和名称空间之间混淆不清。您的Address元素的名称为Address,而不是ns1,其名称空间为http://TestBizTalkMap.Address,而不是ns1:http://TestBizTalkMap.Address

这就是生成正确XML所需的全部内容。

[XmlRoot(Namespace = "http://TestBizTalkMap.Student")]
public class StudentInfo
{
    [XmlElement(Namespace = "")]
    public Student Student { get; set; }
    [XmlElement(Namespace = "http://TestBizTalkMap.Address")]
    public Address Address { get; set; }
}

public class Student
{
    public string EnrollNo { get; set; }
    public string Name { get; set; }
    public string BTSReceivedOn { get; set; }
}

public class Address
{    
    [XmlElement(Namespace = "")]
    public string City { get; set; }        
    [XmlElement(Namespace = "")]
    public string State { get; set; }
}

命名空间前缀并不重要,但如果您真的希望它们是ns0ns1,那么您可以通过传递给XmlSerializerNamespaces的{​​{1}}指定这些前缀Serialize方法:

var ns= new XmlSerializerNamespaces();
ns.Add("ns0", "http://TestBizTalkMap.Student");
ns.Add("ns1", "http://TestBizTalkMap.Address");

如果您不想要XML声明,那么将生成的XML加载到XmlDocument并删除它,只需停止使用它来编写XmlWriterSettings

var settings = new XmlWriterSettings
{
    OmitXmlDeclaration = true,
    // ...
};

有关正常工作的演示,请参阅this fiddle