prosseek
prosseek

Reputation: 190679

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 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>

For example, how can I generate the XML as follows:

<?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>

Upvotes: 3

Views: 3530

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062550

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

Q3: Simply:

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

Q2 re the top level name; you can use

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

Q2 re member names:

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

(see also [XmlAttribute(...)])

Q1 Removing the xsi etc can be done with XmlSerializerNamespaces:

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

Upvotes: 13

Related Questions