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

C# XML Serialisation - Namespace Issue with referenced Schema

我正在将 class 序列化为特定的 XML 格式。下面是我用来序列化它的 class 结构和代码。序列化文件往往会遗漏元素名称 "Address".

我有 3 个 classes StudentInfo,它有两个 "Student" 和 "Address"

类型的属性
[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>

有什么帮助吗?

谢谢

您似乎对名称和命名空间感到非常困惑。 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; }
}

命名空间 prefixes 并不重要,但如果您真的希望它们是 ns0ns1 那么您可以通过 XmlSerializerNamespaces 你传递给 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