C# 对象到 XML

发布于 2024-10-08 05:44:12 字数 1306 浏览 0 评论 0原文

我正在创建一个需要将 C# 对象转换为 XML 的应用程序。

我正在使用 XML Serializer 类来实现此目的。这是代码片段:

public  class Anwer
{
    public int ID { get; set; }
    public string XML { get; set; }
    public Anwer(int ID, string XML)
    {
        this.ID = ID;
        this.XML = XML;
    }
    public Anwer() { }
}

这是主要函数:

   string AnswerXML = @"<Answer>1<Answer>";
   List<Anwer> answerList = new List<Anwer>();
   answerList.Add(new Anwer(1,AnswerXML));
   AnswerXML = @"<Answer>2<Answer>";
   answerList.Add(new Anwer(2, AnswerXML));
   XmlSerializer x = new XmlSerializer(answerList.GetType());
   x.Serialize(Console.Out, answerList);

输出是:

<?xml version="1.0" encoding="IBM437"?>
<ArrayOfAnwer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="h
ttp://www.w3.org/2001/XMLSchema">
  <Anwer>
    <ID>1</ID>
    <XML>&lt;Answer&gt;1&lt;Answer&gt;</XML>
  </Anwer>
  <Anwer>
    <ID>2</ID>
    <XML>&lt;Answer&gt;2&lt;Answer&gt;</XML>
  </Anwer>
</ArrayOfAnwer>

在上面的代码中 '<'和“>”被 '<' 取代和“>”; 如何避免这种情况? 我知道字符串替换是其中一种方法,但我不想使用它。

提前致谢。

I am creating an application which requires to convert c# object to XML.

I am using XML Serializer class to achieve this. Here is the code snippet:

public  class Anwer
{
    public int ID { get; set; }
    public string XML { get; set; }
    public Anwer(int ID, string XML)
    {
        this.ID = ID;
        this.XML = XML;
    }
    public Anwer() { }
}

Here is the main function:

   string AnswerXML = @"<Answer>1<Answer>";
   List<Anwer> answerList = new List<Anwer>();
   answerList.Add(new Anwer(1,AnswerXML));
   AnswerXML = @"<Answer>2<Answer>";
   answerList.Add(new Anwer(2, AnswerXML));
   XmlSerializer x = new XmlSerializer(answerList.GetType());
   x.Serialize(Console.Out, answerList);

The output is:

<?xml version="1.0" encoding="IBM437"?>
<ArrayOfAnwer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="h
ttp://www.w3.org/2001/XMLSchema">
  <Anwer>
    <ID>1</ID>
    <XML><Answer>1<Answer></XML>
  </Anwer>
  <Anwer>
    <ID>2</ID>
    <XML><Answer>2<Answer></XML>
  </Anwer>
</ArrayOfAnwer>

In the above code '<' and '>' are getting replaced by '<' and '>';
How to avoid this?
I know string replace is one of the way, but I don't want to use it.

Thanks in advance.

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

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

发布评论

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

评论(6

旧情勿念 2024-10-15 05:44:29

XmlSerializer 不会相信您的元素是xml,除非您说服它,例如将该属性公开为 XmlDocument。否则,它(正确地,IMO)总是对这些值进行编码。例如:

using System;
using System.Xml;
using System.Xml.Serialization;
public class Anwer
{
    public int ID { get; set; }
    public XmlDocument XML { get; set; }
    public Anwer(int ID, string XML)
    {
        this.ID = ID;
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(XML);
        this.XML = doc;
    }
    public Anwer()
    { }
}
static class Program
{
    static void Main()
    {
        var answer = new Anwer(123, "<Answer>2</Answer>");
        var ser = new XmlSerializer(answer.GetType());
        ser.Serialize(Console.Out, answer);
    }
}

XmlSerializer won't believe you that an element is xml unless you convince it, for example by exposing that property as an XmlDocument. Otherwise, it (correctly, IMO) always encodes such values. For example:

using System;
using System.Xml;
using System.Xml.Serialization;
public class Anwer
{
    public int ID { get; set; }
    public XmlDocument XML { get; set; }
    public Anwer(int ID, string XML)
    {
        this.ID = ID;
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(XML);
        this.XML = doc;
    }
    public Anwer()
    { }
}
static class Program
{
    static void Main()
    {
        var answer = new Anwer(123, "<Answer>2</Answer>");
        var ser = new XmlSerializer(answer.GetType());
        ser.Serialize(Console.Out, answer);
    }
}
余生共白头 2024-10-15 05:44:29

我正在创建一个需要将 C# 对象转换为 XML 的应用程序。我正在使用 XML Serializer 类来实现此目的

如果您使用 XML Serializer 来完成这项工作,那么为什么要在“XML”字段中插入手动编码的 XML?似乎您想要更像这样的东西(使用您的类名,尽管它看起来像是拼写错误):

public class Anwer
{
    public int ID { get; set; }
    public int Answer { get; set; }
}

..

List<Anwer> answerList = new List<Anwer>() {
   new Anwer { ID=1, Answer=2 },
   new Anwer { ID=2, Answer=3 },
};
XmlSerializer x = new XmlSerializer(answerList.GetType());
x.Serialize(Console.Out, answerList);

..

<ArrayOfAnwer ...>
  <Anwer>
    <ID>1</ID>
    <Answer>2</Answer>
  </Anwer>
  ...

或者如果您实际上希望/需要将 Answer 元素出于某种原因嵌套在 XML 元素中,您可以将 Anwer 对象更改为反映该结构(如 Oleg Kalenchuk 建议的那样),或者自己生成 XML,而不是使用序列化程序:

XElement xml = new XElement("AnwerList", 
   from anwer in anwerList select
      new XElement("Anwer",
         new XElement("ID", anwer.ID),
         new XElement("XML",
            new XElement("Answer", anwer.Answer)
            )
         )
      );
Console.Out.WriteLine(xml);

<AnwerList>
  <Anwer>
    <ID>1</ID>
    <XML>
      <Answer>2</Answer>
    </XML>
  </Anwer>
  ...

无论如何,我更喜欢后者,因为它为您提供了更多控制权。

I am creating an application which requires to convert c# object to XML. I am using XML Serializer class to achieve this

If you're using the XML Serializer to do the work, then why the "XML" field where you're inserting hand-coded XML? Seems like you want something more like this (using your class name, though it looks like a misspelling):

public class Anwer
{
    public int ID { get; set; }
    public int Answer { get; set; }
}

..

List<Anwer> answerList = new List<Anwer>() {
   new Anwer { ID=1, Answer=2 },
   new Anwer { ID=2, Answer=3 },
};
XmlSerializer x = new XmlSerializer(answerList.GetType());
x.Serialize(Console.Out, answerList);

..

<ArrayOfAnwer ...>
  <Anwer>
    <ID>1</ID>
    <Answer>2</Answer>
  </Anwer>
  ...

Or if you actually want/need the Answer element to be nested in an XML element for some reason, you can alter your Anwer object to reflect that structure (as Oleg Kalenchuk suggests), or generate the XML yourself rather than using the serializer:

XElement xml = new XElement("AnwerList", 
   from anwer in anwerList select
      new XElement("Anwer",
         new XElement("ID", anwer.ID),
         new XElement("XML",
            new XElement("Answer", anwer.Answer)
            )
         )
      );
Console.Out.WriteLine(xml);

<AnwerList>
  <Anwer>
    <ID>1</ID>
    <XML>
      <Answer>2</Answer>
    </XML>
  </Anwer>
  ...

I prefer the latter anyway, because it gives you more control.

安穩 2024-10-15 05:44:29

您正在分配一个包含 < 的字符串和>对 XML 元素进行签名,因此很明显序列化程序将替换 <和>带有实体引用。即使您得到>在文本中,当您反序列化 XML 时,您将得到 >在你的文字中。

You're assigning a string containing the < and > sign to the XML element so it is obvious that teh serializer would replace the < and > with entity references. Even if you're getting > in the text when you deserialise the XML you'll get the > in your text.

划一舟意中人 2024-10-15 05:44:29
  1. 使用一个整数“Answer”成员创建一个新类 AnswerXML
  2. 将 XML 成员的类型更改为 AnswerXML 而不是字符串
  1. Create a new class AnswerXML with one integer "Answer" member
  2. Change type of XML member to AnswerXML instead of string
总以为 2024-10-15 05:44:29

因为“<”和“>”是用于 xml 结构本身的字符,它们会自动进行 html 编码。当您在应用程序中读回它并反序列化它时,'<''>' 应转换回 '<'和“>”。

如果您的目标不是这样,请使用 htmldecode 功能。

如果这没有帮助,只需告诉您到底想对 xml 数据做什么。

Because '<' and '>' are characters used for the xml-structure itself, they are automatically htmlencoded. When you read it back in your app and deserialize it, the '<' and '>' should be converted back to '<' and '>'.

If your goal is otherwise, use htmldecode functionality.

If this don't help, just tell what exactly you want to do with the xml-data.

胡渣熟男 2024-10-15 05:44:28

基本上你不知道。这是正确序列化对象的方法 - XML 序列化程序不想处理字符串中的 XML 造成的混乱,因此它会适当地转义 XML。

如果稍后反序列化 XML,您将返回到原始对象数据。

如果您尝试以自定义方式构建 XML 文档,我建议您一开始就不要使用 XML 序列化。如果您愿意显式创建元素等,请使用 LINQ to XML,或者如果您真的想要直接在输出中包含任意字符串,请使用 XmlWriter

如果您可以向我们提供有关您正在尝试执行的操作的更多信息,我们也许能够提出更好的替代方案 - 直接构建 XML 字符串几乎从来都不是一个好主意。

You don't, basically. That's correctly serializing the object - the XML serializer doesn't want to have to deal with XML within strings messing things up, so it escapes the XML appropriately.

If you deserialize the XML later, you'll get back to the original object data.

If you're trying to build up an XML document in a custom fashion, I suggest you don't use XML serialization to start with. Either use LINQ to XML if you're happy to create elements etc explicitly, or if you really, really want to include arbitrary strings directly in your output, use XmlWriter.

If you could give us more information about the bigger picture of what you're trying to do, we may be able to suggest better alternatives - building XML strings directly is almost never a good idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文