从具有特定属性的C#类生成XML
我遇到了一个关于c#中类的XML序列化的问题:我想要的是以下格式的XML从具有特定属性的C#类生成XML,c#,.net,xml,xml-serialization,C#,.net,Xml,Xml Serialization,我遇到了一个关于c#中类的XML序列化的问题:我想要的是以下格式的XML <?xml version="1.0" encoding="utf-8"?> <MyClass:Req xsi:schemaLocation="http://www.Test.com/MyClass.xsd" xmlns:TestClass="http://www.Test.com/MyClass" xmlns:xsi="http://www.w3.org
<?xml version="1.0" encoding="utf-8"?>
<MyClass:Req xsi:schemaLocation="http://www.Test.com/MyClass.xsd"
xmlns:TestClass="http://www.Test.com/MyClass"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.Test.com/MyClass">
<head>
<hA>01</hA>
</head>
<body>
<R1>0</R1>
<R3 />
<R4 />
</body>
</MyClass:Req>
我知道我需要更改XMLRoot行以获得所需的输出,但我不确定我需要在这里更改什么
另外,在将冒号写入文件之前,如何强制保留冒号,而不是将其修改为x003A
最后一个问题是,由于我没有填充R3和R4,如果没有填充值,那么有没有一种方法可以防止将它们写入XML,就像R2没有写入文件一样,我认为区别在于R2是一个字符串,而R3和R4是复杂类型,这使得序列化程序更难确定是否应该写入它们
编辑:
确定应用Ralp建议的更改后,文件现在看起来像:
<?xml version="1.0" encoding="utf-8"?>
<MyClass:Req d1p1:schemaLocation="http://www.test.com/myclass.xsd"
xmlns:d1p1="http://www.w3.org/2001/XMLSchema-instance"
xmlns:MyClass="http://www.test.com/myclass">
<head>
<hA>01</hA>
</head>
<body>
<R1>0</R1>
<R3 />
<R4 />
</body>
</MyClass:Req>
现在唯一的问题是缺少xmlns=”行http://www.Test.com/MyClass“>
如果将以下代码添加到命名空间部分:
ns.Add("", "http://www.test.com/Myclass");
我得到了所需的输出,但请注意,jhad更改了其中一个字母的大小写以获得输出,如果大小写与名称空间MyClass相同,则不会输出
提前感谢名称空间是MyClass,Elementname是Req。 因此,您需要定义名称空间
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("MyClass", "http://www.test.com/myclass");
并调用接受XmlSerializerNamespaces实例的序列化重载。
请求类本身就是一个名为Req的元素
[XmlRoot(Namespace = "http://www.test.com/myclass", ElementName = "Req")]
public class Request
{
[XmlElement("R1", Namespace = "")]
public string R1 { get; set; }
public bool ShouldSerializeR1() { return !string.IsNullOrWhiteSpace(R1); }
[XmlAttribute("schemaLocation", Namespace = "")]
public string xsiSchemaLocation = "http://www.test.com/myclass.xsd";
}
可以通过声明为Xml属性的属性添加schemaLocation。
如果属性应该序列化,则可以使用ShouldSerialize*MyLovelyPropertyName()*语法的方法控制该属性。谢谢Ralf,我现在得到的结果是:01 0 d1p1是伪造的名称空间。如果需要某个名称空间,请定义它<代码>ns.Add(“xsi”http://www.w3.org/2001/XMLSchema-instance");代码>可能有帮助。也许您还需要添加一个没有名称(空字符串)的名称空间,或者最好只为SchemaLocation定义一个空名称空间。我将相应地编辑代码。感谢Ralf,ns.Add(“xsi”),修复了d1p1的问题,现在唯一遗漏的是xmlns=“”>您是否尝试添加一个无名(因此希望是默认)名称空间来获取它?
ns.Add(“,“Test.com/MyClass”);
我已将Ralp的答案标记为已接受的答案,因为他的更改帮助我获得了99%的解决方案。我现在唯一想做的就是将xmlns=“”>添加到文件中
request theReq = new request();
requestHead rHead = new requestHead();
requestBody rBody = new requestBody();
rHead.hA = "01";
rBody.R1 = "0";
theReq.head = rHead;
theReq.body = rBody;
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
ns.Add("MyClass", "http://www.test.com/myclass");
XmlSerializer serializer = new XmlSerializer(typeof(request));
TextWriter textWriter = new StreamWriter(@"Test.xml");
serializer.Serialize(textWriter, theReq, ns);
textWriter.Close();
[XmlRoot(Namespace = "http://www.test.com/myclass", ElementName = "Req")]
public partial class request
{
private requestHead headField;
private requestBody bodyField;
public request()
{
this.bodyField = new requestBody();
this.headField = new requestHead();
}
[XmlAttribute("schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string xsiSchemaLocation = "http://www.test.com/myclass.xsd";
[XmlElement("head", Namespace = "")]
public requestHead head
{
get
{
return this.headField;
}
set
{
this.headField = value;
}
}
[XmlElement("body", Namespace = "")]
public requestBody body
{
get
{
return this.bodyField;
}
set
{
this.bodyField = value;
}
}
}
public partial class requestHead
{
private string headAField;
private string headBField;
public string hA
{
get
{
return this.headAField;
}
set
{
this.headAField = value;
}
}
public string hB
{
get
{
return this.headBField;
}
set
{
this.headBField = value;
}
}
}
public partial class requestBody
{
private string R1Field;
private string R2Field;
private requestBodyR3 R3Field;
private requestBodyR4 R4Field;
public requestBody()
{
this.R4Field = new requestBodyR4();
this.R3Field = new requestBodyR3();
}
public string R1
{
get
{
return this.R1Field;
}
set
{
this.R1Field = value;
}
}
public bool ShouldSerializeR1() { return !String.IsNullOrEmpty(R1); }
public string R2
{
get
{
return this.R2Field;
}
set
{
this.R2Field = value;
}
}
public bool ShouldSerializeR2() { return !String.IsNullOrEmpty(R2); }
public requestBodyR3 R3
{
get
{
return this.R3Field;
}
set
{
this.R3Field = value;
}
}
public bool ShouldSerializeR3() { return requestBodyR3.ShouldSerializeRequestBodyR3(); }
public requestBodyR4 R4
{
get
{
return this.R4Field;
}
set
{
this.R4Field = value;
}
}
}
public partial class requestBodyR3
{
private string R31Field;
private string R32Field;
public static bool ShouldSerializeRequestBodyR3()
{
bool blnRetVal = false;
//Add code to test these values later
return blnRetVal;
}
public string R31
{
get
{
return this.R31Field;
}
set
{
this.R31Field = value;
}
}
public string R32
{
get
{
return this.R32Field;
}
set
{
this.R32Field = value;
}
}
}
public partial class requestBodyR4
{
private string R41Field;
public string R41
{
get
{
return this.R41Field;
}
set
{
this.R41Field = value;
}
}
}
ns.Add("", "http://www.test.com/Myclass");
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("MyClass", "http://www.test.com/myclass");
[XmlRoot(Namespace = "http://www.test.com/myclass", ElementName = "Req")]
public class Request
{
[XmlElement("R1", Namespace = "")]
public string R1 { get; set; }
public bool ShouldSerializeR1() { return !string.IsNullOrWhiteSpace(R1); }
[XmlAttribute("schemaLocation", Namespace = "")]
public string xsiSchemaLocation = "http://www.test.com/myclass.xsd";
}