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

C#XML序列化-引用架构的命名空间问题,c#,xml-serialization,C#,Xml Serialization,我正在将类序列化为特定的XML格式。下面是我用来序列化它的类结构和代码。序列化文件往往会丢失元素名称“地址” 我有三个类StudentInfo,它有两个属性类型“Student”和“Address” 用于序列化的代码: public XmlDocument GetXMLSchema<T>(T type, string schemeNameSpace) { XmlDocument xmlDoc = new XmlDocument(); XmlSerializer xmlSe

我正在将类序列化为特定的XML格式。下面是我用来序列化它的类结构和代码。序列化文件往往会丢失元素名称“地址”

我有三个类StudentInfo,它有两个属性类型“Student”和“Address”

用于序列化的代码:

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;
     }
公共XmlDocument GetXMLSchema(T类型,字符串schemeNameSpace) { XmlDocument xmlDoc=新的XmlDocument(); XmlSerializer XmlSerializer=新的XmlSerializer(type.GetType()); 尝试 { 使用(MemoryStream xmlStream=new MemoryStream()) { XmlSerializerNamespaces ns=新的XmlSerializerNamespaces(); 添加(“ns0”,schemeNameSpace); 序列化(xmlStream,类型,ns); xmlStream.Position=0; 加载(xmlStream); if(xmlDoc.FirstChild.NodeType==XmlNodeType.xmldocation) { xmlDoc.RemoveChild(xmlDoc.FirstChild); } } } 捕获(例外情况除外) { 掷骰子; } 返回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

注册
名称
BTSReceivedOn
城市
陈述

您似乎对名称和名称空间感到非常困惑。
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; }
}
名称空间前缀并不重要,但如果确实希望它们是
ns0
ns1
,则可以通过传递给
Serialize
方法的
XmlSerializerNamespaces
指定它们:

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,
    // ...
};

请参阅,以获取可用的演示

您是否尝试过将Address属性上的
XmlElement(“ns1”,Namespace=“…”)
更改为
XmlElement(“Address”,Namespace=“…”)
?看起来很明显,但我想我会检查一下
var ns= new XmlSerializerNamespaces();
ns.Add("ns0", "http://TestBizTalkMap.Student");
ns.Add("ns1", "http://TestBizTalkMap.Address");
var settings = new XmlWriterSettings
{
    OmitXmlDeclaration = true,
    // ...
};