在C#中更改XmlSerializer生成的XML结构

时间:2011-02-16 07:02:18

标签: c# xml xml-serialization

我的课程如下

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 = "";
        }

使用以下序列化代码

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();

我可以获得以下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>
  • 问题1:如何删除xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://...以包含简单元素<Module>..</Module>
  • Q2:XML元素名称与类名称或变量名称完全相同。我可以用自己的吗?
  • 替换它
  • 问题3:我可以删除外<ClassInfoList>

例如,如何生成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>

1 个答案:

答案 0 :(得分:13)

(顺便说一句,它与问题没有关系,但你的目标应该是避免使用公共字段,因为许多stackoverflow问题都涉及很多原因)

问题3:简单地说:

[XmlElement("Class")]
public List<ClassInfo> ClassInfoList;

Q2成为顶级名称;你可以用

[XmlRoot("somethingFun")]
public class Module { ... }

Q2重新成员名称:

[XmlElement("blocks")]
public int BlocksCovered;

(另见[XmlAttribute(...)]

Q1删除xsi等可以使用XmlSerializerNamespaces

完成
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
var ser = new XmlSerializer(typeof(Module));
ser.Serialize(destination, module, ns);