0
我有一個帶有按鈕的網頁。點擊後,我將我的變量傳遞給一個類來創建一個xml文件。我試圖在名爲Message的元素下添加我的名稱空間。它工作不正常。序列化爲xml和名稱空間放置
這裏是我的類:
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.0.30319.1.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Request
{
private string sNameField;
/// <remarks/>
public string SName
{
get
{
return this.sNameField;
}
set
{
this.sNameField = value;
}
}
/// <remarks/>
public Message Message
{
get
{
return this.messageField;
}
set
{
this.messageField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = false)]
public partial class Message
{
private AddO addOField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://pd.com")]
public AddO AddO
{
get
{
return this.addOField;
}
set
{
this.addOField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://pd.com")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://pd.com", IsNullable = false)]
public partial class AddO
{
private CaseD caseD;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.a.com")]
public CaseD CaseD
{
get
{
return this.caseDField;
}
set
{
this.caseDField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.a.com")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.a.com", IsNullable = false)]
public partial class CaseD
{
private CaseA caseAField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.z.com")]
public CaseA CaseA
{
get
{
return this.caseAField;
}
set
{
this.caseAField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.z.com")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.z.com", IsNullable = false)]
public partial class CaseA
{
private Doc docField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.a.com")]
public DocuE DocE
{
get
{
return this.docEField;
}
set
{
this.docEField = value;
}
}
}
這裏是我的按鈕事件:
<?xml version="1.0" encoding="utf-8"?>
<Request xmlns:a="http://www.a.com">
<SName>hi</SName>
<Message>
<addO xmlns="http://pd.com">
<a:caseD>
<caseA xmlns="http://www.z.com">
</caseA>
</a:caseD>
</addO>
</Message>
</Request>
我需要的命名空間是僅在:
caseA caseA = new caseA();
caseD caseD = new caseD();
caseD.caseA = caseA;
addO addO = new addO();
addO.caseD = caseD;
Message Message = new Message();
Message.addO = addO;
Request Request = new Request();
Request.Message = Message;
Request.SName = "Hi";
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("z", "http://www.z.com");
ns.Add("pd", "http://pd.com");
ns.Add("a", "http://www.a.com");
XmlSerializer serializer = new XmlSerializer(typeof(Request), new Type[] {typeof(Message), typeof(addO), typeof(caseD), typeof(caseA)});
StreamWriter writer = new StreamWriter(Server.MapPath("~/images/test.xml"));
serializer.Serialize(writer, Request, ns);
我在XML得到這個Message元素:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Request>
<SName>Hi</SName>
<Message>
<pd:addO
xmlns:a="http://www.a.com"
xmlns:z="http://www.z.com"
xmlns:pd="http://pd.com">
<a:caseD>
<z:caseA>
</z:caseA>
</a:caseD>
</pd:addO>
</Message>
</Request>
您可能需要使用'XmlWriter'仔細寫出來的XML你喜歡的方式。請注意,由於兩個版本都與XML的觀點相同,因此.Net中沒有太多提示移動前綴聲明的位置。 –
你的測試用例不能編譯 - 'Doc'和'DocuE'類型丟失,字段'Request.messageField'丟失。你可以創建一個[Minimal,Complete,and Verifiable example](http://stackoverflow.com/help/mcve)你的問題嗎? – dbc