[英]Changing the XML structure generated by XmlSerializer in C#
I have classes as follows 我的课程如下
namespace Coverage {
public class ClassInfo {
public string ClassName;
public int BlocksCovered;
public int BlocksNotCovered;
public ClassInfo() {}
public ClassInfo(string ClassName, int BlocksCovered, int BlocksNotCovered)
{
this.ClassName = ClassName;
this.BlocksCovered = BlocksCovered;
this.BlocksNotCovered = BlocksNotCovered;
}
}
public class Module {
public List<ClassInfo> ClassInfoList;
public int BlocksCovered;
public int BlocksNotCovered;
public string moduleName;
public Module()
{
ClassInfoList = new List<ClassInfo>();
BlocksCovered = 0;
BlocksNotCovered = 0;
moduleName = "";
}
With the following serializer code 使用以下序列化程序代码
XmlSerializer SerializerObj = new XmlSerializer(typeof(Module));
// Create a new file stream to write the serialized object to a file
TextWriter WriteFileStream = new StreamWriter(@"test.xml");
SerializerObj.Serialize(WriteFileStream, report);
WriteFileStream.Close();
I can get the following XML file. 我可以获得以下XML文件。
<?xml version="1.0" encoding="utf-8"?>
<Module xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ClassInfoList>
<ClassInfo>
<ClassName>Fpga::TestMe</ClassName>
<BlocksCovered>4</BlocksCovered>
<BlocksNotCovered>8</BlocksNotCovered>
</ClassInfo>
<ClassInfo>
<ClassName>Fpga::TestMe2</ClassName>
<BlocksCovered>4</BlocksCovered>
<BlocksNotCovered>8</BlocksNotCovered>
</ClassInfo>
</ClassInfoList>
<BlocksCovered>8</BlocksCovered>
<BlocksNotCovered>16</BlocksNotCovered>
<moduleName>helloclass.exe</moduleName>
</Module>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://...
to have simple element <Module>..</Module>
? Q1:我如何删除xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://...
以使简单元素<Module>..</Module>
? <ClassInfoList>
? Q3:我可以删除外部的<ClassInfoList>
吗? For example, how can I generate the XML as follows: 例如,如何生成XML,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Module>
<Class>
<ClassName>Fpga::TestMe</ClassName>
<BlocksCovered>4</BlocksCovered>
<BlocksNotCovered>8</BlocksNotCovered>
</Class>
<Class>
<ClassName>Fpga::TestMe2</ClassName>
<BlocksCovered>4</BlocksCovered>
<BlocksNotCovered>8</BlocksNotCovered>
</Class>
<BlocksCovered>8</BlocksCovered>
<BlocksNotCovered>16</BlocksNotCovered>
<moduleName>helloclass.exe</moduleName>
</Module>
(btw, it doesn't tie to the question, but you should aim to avoid public fields, for lots of reasons covered in many stackoverflow questions) (顺便说一下,它与问题无关,但你应该以避免公共字段为目标,因为许多stackoverflow问题都涉及很多原因)
Q3: Simply: Q3:简单地说:
[XmlElement("Class")]
public List<ClassInfo> ClassInfoList;
Q2 re the top level name; Q2成为顶级名称; you can use 您可以使用
[XmlRoot("somethingFun")]
public class Module { ... }
Q2 re member names: Q2重新成员名称:
[XmlElement("blocks")]
public int BlocksCovered;
(see also [XmlAttribute(...)]
) (另见[XmlAttribute(...)]
)
Q1 Removing the xsi etc can be done with XmlSerializerNamespaces
: Q1删除xsi等可以使用XmlSerializerNamespaces
完成:
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
var ser = new XmlSerializer(typeof(Module));
ser.Serialize(destination, module, ns);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.