XmlSerializer 在 XML 文件中具有属性和前缀

发布于 2024-12-20 10:06:11 字数 2655 浏览 3 评论 0原文

我可以使用 XmlSerializer 从 C# 类获取此 XML 文件。

<?xml version="1.0" encoding="utf-8"?>
<Component xmlns:spirit="b" xmlns:chrec="a" MovieName="0" BlocksNotCovered="0">
  <ClassInfoList>
    <chrec:string>hello</chrec:string>
    <chrec:string>world</chrec:string>
  </ClassInfoList>
  <moduleName />
  <world>
    <x>10</x>
    <y>20</y>
  </world>
</Component>

如何为 chrec 和 spriti 添加前缀命名空间?例如,我怎样才能得到这个XML文件?

<?xml version="1.0" encoding="utf-8"?>
<spirit:Component xmlns:spirit="b" xmlns:chrec="a" MovieName="0" BlocksNotCovered="0">
  <spirit:ClassInfoList>
    <chrec:string>hello</chrec:string>
    <chrec:string>world</chrec:string>
  </spirit:ClassInfoList>
  <spirit:moduleName />
  <chrec:world>
    <chrec:x>10</chrec:x>
    <chrec:y>20</chrec:y>
  </chrec:world>
</spirit:Component>

这是 C# 代码。

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.Linq;

namespace Coverage
{
    public class Hello
    {
        public int x;
        public int y;
        public Hello()
        {
            x = 10;
            y = 20;
        }
    }
    public class Component {
        [XmlAttribute("MovieName")]
        public int MovieName;
        [XmlAttribute("BlocksNotCovered")]
        public int BlocksNotCovered;
        [XmlNamespaceDeclarations]
        public XmlSerializerNamespaces ns;

        public List<string> ClassInfoList;

        public string moduleName;
        public Hello world;

        public Component()
        {
            ClassInfoList = new List<string>() {"hello", "world"};
            MovieName = 0;
            BlocksNotCovered = 0;
            moduleName = "";
            world = new Hello();
        }
    }

    class Cov2xml
    {
        static void Main(string[] args)
        {
            string xmlFileName = "perf.xml";
            Component report = new Component();

            TextWriter writeFileStream = new StreamWriter(xmlFileName);

            report.ns = new XmlSerializerNamespaces();
            report.ns.Add("chrec","a");
            report.ns.Add("spirit","b");

            var ser = new XmlSerializer(typeof(Component));
            ser.Serialize(writeFileStream, report, report.ns);
            writeFileStream.Close();            
        }
    }
}

I could get this XML file from C# class with XmlSerializer.

<?xml version="1.0" encoding="utf-8"?>
<Component xmlns:spirit="b" xmlns:chrec="a" MovieName="0" BlocksNotCovered="0">
  <ClassInfoList>
    <chrec:string>hello</chrec:string>
    <chrec:string>world</chrec:string>
  </ClassInfoList>
  <moduleName />
  <world>
    <x>10</x>
    <y>20</y>
  </world>
</Component>

How can I add prefix namespaces for chrec and spriti? For example, how can I get this XML file?

<?xml version="1.0" encoding="utf-8"?>
<spirit:Component xmlns:spirit="b" xmlns:chrec="a" MovieName="0" BlocksNotCovered="0">
  <spirit:ClassInfoList>
    <chrec:string>hello</chrec:string>
    <chrec:string>world</chrec:string>
  </spirit:ClassInfoList>
  <spirit:moduleName />
  <chrec:world>
    <chrec:x>10</chrec:x>
    <chrec:y>20</chrec:y>
  </chrec:world>
</spirit:Component>

This is the C# code.

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.Linq;

namespace Coverage
{
    public class Hello
    {
        public int x;
        public int y;
        public Hello()
        {
            x = 10;
            y = 20;
        }
    }
    public class Component {
        [XmlAttribute("MovieName")]
        public int MovieName;
        [XmlAttribute("BlocksNotCovered")]
        public int BlocksNotCovered;
        [XmlNamespaceDeclarations]
        public XmlSerializerNamespaces ns;

        public List<string> ClassInfoList;

        public string moduleName;
        public Hello world;

        public Component()
        {
            ClassInfoList = new List<string>() {"hello", "world"};
            MovieName = 0;
            BlocksNotCovered = 0;
            moduleName = "";
            world = new Hello();
        }
    }

    class Cov2xml
    {
        static void Main(string[] args)
        {
            string xmlFileName = "perf.xml";
            Component report = new Component();

            TextWriter writeFileStream = new StreamWriter(xmlFileName);

            report.ns = new XmlSerializerNamespaces();
            report.ns.Add("chrec","a");
            report.ns.Add("spirit","b");

            var ser = new XmlSerializer(typeof(Component));
            ser.Serialize(writeFileStream, report, report.ns);
            writeFileStream.Close();            
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰雪之触 2024-12-27 10:06:11

感谢有能力的_tech 的链接,我可以找到方法。

如何设置前缀命名空间?

可以使用XmlRootAttribute,重要的是名称空间是命名空间,而不是命名空间名称。在示例中,它应该是“b”而不是“chrec”。

[XmlRootAttribute("Component", Namespace="http://namespace", IsNullable = false)]
public class Component {

如何为特定元素设置前缀命名空间?

您可以在变量之前使用 XmlElement。

[XmlElement("xyz", Namespace="http://www.namespace", IsNullable = false)]
int x;

你会得到这个。

<?xml version="1.0" encoding="utf-8"?>
<chrec:Component xmlns:spirit="http:..." MovieName="0" BlocksNotCovered="0" xmlns:chrec="...">
  <chrec:ClassInfoList>
    <chrec:string>hello</chrec:string>
    <chrec:string>world</chrec:string>
  </chrec:ClassInfoList>
  <chrec:moduleName />
  <chrec:world>
    <spirit:xyz>10</spirit:xyz>
    <chrec:y>20</chrec:y>
  </chrec:world>
</chrec:Component>

Thanks to the link from competent_tech, I could figure out the way to do it.

How to set the prefix namespace?

You can use XmlRootAttribute, the important thing is that the names space is the namespace, not the namespace name. In the example, it should be "b" not "chrec".

[XmlRootAttribute("Component", Namespace="http://namespace", IsNullable = false)]
public class Component {

How to set the prefix namespace for a specific element?

You can use XmlElement just before the variable.

[XmlElement("xyz", Namespace="http://www.namespace", IsNullable = false)]
int x;

And you'll get this.

<?xml version="1.0" encoding="utf-8"?>
<chrec:Component xmlns:spirit="http:..." MovieName="0" BlocksNotCovered="0" xmlns:chrec="...">
  <chrec:ClassInfoList>
    <chrec:string>hello</chrec:string>
    <chrec:string>world</chrec:string>
  </chrec:ClassInfoList>
  <chrec:moduleName />
  <chrec:world>
    <spirit:xyz>10</spirit:xyz>
    <chrec:y>20</chrec:y>
  </chrec:world>
</chrec:Component>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文