更改C#XmlSerializer的所生成的XML结构 [英] Changing the XML structure generated by XmlSerializer in C#

查看:166
本文介绍了更改C#XmlSerializer的所生成的XML结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上课如下:

 命名空间覆盖{
公共类的ClassInfo {
公共字符串类名;
公众诠释BlocksCovered;
公众诠释BlocksNotCovered;

公众的ClassInfo(){}

公众的ClassInfo(串类名,诠释BlocksCovered,诠释BlocksNotCovered)
{
this.ClassName =类名;
this.BlocksCovered = BlocksCovered;
this.BlocksNotCovered = BlocksNotCovered;
}
}

公共类模块{
公开名单<&的ClassInfo GT; ClassInfoList;
公众诠释BlocksCovered;
公众诠释BlocksNotCovered;
公共字符串MODULENAME;

公共模块()
{
ClassInfoList =新的List<&的ClassInfo GT;();
BlocksCovered = 0;
BlocksNotCovered = 0;
MODULENAME =;
}

通过以​​下串行代码



  XmlSerializer的SerializerObj =新的XmlSerializer(typeof运算(模块)); 
//创建一个新的文件流写入序列化对象到一个文件
的TextWriter WriteFileStream =新的StreamWriter(@的test.xml);
SerializerObj.Serialize(WriteFileStream,报告);
WriteFileStream.Close();



我可以得到下面的XML文件。

 <?XML版本=1.0编码=UTF-8>?; 
<模块的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance的xmlns:XSD =htt​​p://www.w3.org/2001/XMLSchema>
< ClassInfoList>
<&的ClassInfo GT;
<类名> FPGA :: TESTME< /类名>
将; BlocksCovered→4&下; / BlocksCovered>
< BlocksNotCovered> 8示/ BlocksNotCovered>
< /&的ClassInfo GT;
<&的ClassInfo GT;
<类名> FPGA :: TestMe2< /类名>
将; BlocksCovered→4&下; / BlocksCovered>
< BlocksNotCovered> 8示/ BlocksNotCovered>
< /&的ClassInfo GT;
< / ClassInfoList>
< BlocksCovered> 8示/ BlocksCovered>
< BlocksNotCovered> 16 LT; / BlocksNotCovered>
<&MODULENAME GT; helloclass.exe< / MODULENAME>
< /模块>




  • Q1:我怎样才能删除的xmlns :XSI =htt​​p://www.w3.org/2001/XMLSchema-instance的xmlns:XSD =HTTP:// ... 用简单的元素< ;模块> ..< /模块>

  • Q2:?XML元素名称是完全一样的类名或变量名我可以代替它与我自己的

  • Q3:?我可以去除外层< ClassInfoList>



例如,我怎么能生成XML如下:

  < XML版本=1.0编码=UTF-8> 
<模块>
<类>
<类名> FPGA :: TESTME< /类名> ;
< BlocksCovered> 4℃; / BlocksCovered>
< BlocksNotCovered> 8< / BlocksNotCovered>
< /类>
<类>
<类名> FPGA :: TestMe2&下; /类名>
将; BlocksCovered→4&下; / BlocksCovered>
将; BlocksNotCovered→8&下; / BlocksNotCovered>
< /类>
< BlocksCovered> 8示/ BlocksCovered>
< BlocksNotCovered> 16 LT; / BlocksNotCovered>
<&MODULENAME GT; helloclass.exe< / MODULENAME>
< /模块>


解决方案

(顺便说一句,它不配合的问题,但你的目标应该是避免公共领域,为大量的涉及许多计算器问题的原因)



Q3:简单:

  [的XmlElement(类)] 
公开名单<&的ClassInfo GT; ClassInfoList;



Q2重新顶级名;你可以使用



  [XmlRoot(somethingFun)] 
公共类模块{...}

Q2重新成员名称:

  [的XmlElement(块)] 
公众诠释BlocksCovered;



(见 [XmlAttribute(...)]



Q1取出XSI等可与 XmlSerializerNamespaces 来完成:

  XmlSerializerNamespaces NS =新XmlSerializerNamespaces(); 
ns.Add(,);
变种SER =新的XmlSerializer(typeof运算(模块));
ser.Serialize(目的地,模块,纳秒);


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>

  • Q1 : How can I remove the xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://... to have simple element <Module>..</Module>?
  • Q2 : The XML element name is exactly the same as the class name or variable name. Can I replace it with my own?
  • Q3 : Can I remove the outer <ClassInfoList>?

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>

解决方案

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

这篇关于更改C#XmlSerializer的所生成的XML结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆