HTML 的 XML 序列化

发布于 2024-08-06 22:13:15 字数 3394 浏览 10 评论 0原文

好吧,这个做到了!谢谢大家!

public class Result
{
    public String htmlEscaped
    {
        set;
        get;
    }

    [XmlIgnore]
    public String htmlValue
    { set; get; }

    [XmlElement("htmlValue")]
    public XmlCDataSection htmlValueCData
    {
        get
        {
            XmlDocument _dummyDoc = new XmlDocument();
            return _dummyDoc.CreateCDataSection(htmlValue);
        }
        set { htmlValue = (value != null) ? value.Data : null; }
    }
}

    Result r = new Result();
    r.htmlValue = ("<b>Hello</b>");
    r.htmlEscaped = ("<b>Hello</b>");
    XmlSerializer xml = new XmlSerializer(r.GetType());
    TextWriter file = new StreamWriter(Environment.CurrentDirectory + "\\results\\result.xml", false, System.Text.Encoding.Default);
    xml.Serialize(file, r);
    file.Close();

结果:

<?xml version="1.0" encoding="Windows-1252"?>
<Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <htmlEscaped>&lt;b&gt;Hello&lt;/b&gt;</htmlEscaped>
  <htmlValue><![CDATA[<b>Hello</b>]]></htmlValue>
</Result>

如您所见,在 CDATA 为返回类型之后,文件系统上的 XML 文件中不再有转义的 html。 JSON 序列化不再起作用,但这可以通过一些类型扩展来修复。


QUESTION WAS:

也许有人知道如何做到这一点...

我有这个类:

public class Result
{
    public String htmlValue
    {
        get;
        set;
    }
}

我用这个将其序列化为 XML

Result res = new Result();
res.htmlValue = "<p>Hello World</p>";
XmlSerializer s = new XmlSerializer(res.GetType());
TextWriter w = new StreamWriter(Environment.CurrentDirectory + "\\result.xml", false, System.Text.Encoding.Default);
s.Serialize(w, res);
w.Close();

Works 很好我得到这个:

<?xml version="1.0" encoding="Windows-1252"?>
<Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <htmlValue>&lt;b&gt;Hello World&lt;/b&gt;</htmlValue>
</Result>

我可以做什么才能得到这个:

<?xml version="1.0" encoding="Windows-1252"?>
<Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <htmlValue><![CDATA[<b>Hello World</b>]]></htmlValue>
</Result>

我已经搜索过,但我可以'找不到任何东西。 htmlValue 的类型 必须保留字符串,因为其他序列化 JSON 等。

棘手的一个...提前感谢建议

  • HTML 在 C# 中的字符串中是正确的。为什么要解码或编码?
  • XmlSerializer 将转义的 HTML 保存到 XML 文件。
  • 不要使用 C# 进行消费。

是接受这个的外部工具:

<htmlValue><![CDATA[<b>Hello World</b>]]></htmlValue>

<htmlValue>&lt;b&gt;Hello World&lt;/b&gt;</htmlValue>

我对 JSON Serializer 不做同样的事情,在硬盘驱动器上的文件中,HTML 保存正确。 为什么以及在哪里使用 HTTP 实用程序来防止这种情况?以及如何获取 周围的内容。

你能给一个代码示例吗? 除了 C# 自己的序列化程序之外,还有其他序列化程序吗?

我找到了这个链接 .NET XML Serialization of CDATA ATTRIBUTE ,我需要这样做,但它是不同的,如何在不更改类型的情况下包含它?

Okay this one DID it! Thanks to all of you!

public class Result
{
    public String htmlEscaped
    {
        set;
        get;
    }

    [XmlIgnore]
    public String htmlValue
    { set; get; }

    [XmlElement("htmlValue")]
    public XmlCDataSection htmlValueCData
    {
        get
        {
            XmlDocument _dummyDoc = new XmlDocument();
            return _dummyDoc.CreateCDataSection(htmlValue);
        }
        set { htmlValue = (value != null) ? value.Data : null; }
    }
}

    Result r = new Result();
    r.htmlValue = ("<b>Hello</b>");
    r.htmlEscaped = ("<b>Hello</b>");
    XmlSerializer xml = new XmlSerializer(r.GetType());
    TextWriter file = new StreamWriter(Environment.CurrentDirectory + "\\results\\result.xml", false, System.Text.Encoding.Default);
    xml.Serialize(file, r);
    file.Close();

RESULT:

<?xml version="1.0" encoding="Windows-1252"?>
<Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <htmlEscaped><b>Hello</b></htmlEscaped>
  <htmlValue><![CDATA[<b>Hello</b>]]></htmlValue>
</Result>

As you can see, after CDATA is return type, no more escaped html in XML file on filesystem.
The JSON Serialization isn't working anymore, but this can be fixed with a little type extention.


QUESTION WAS:

Maybe someone knows how to make do it...

I have this Class:

public class Result
{
    public String htmlValue
    {
        get;
        set;
    }
}

I use this to serialize it to XML

Result res = new Result();
res.htmlValue = "<p>Hello World</p>";
XmlSerializer s = new XmlSerializer(res.GetType());
TextWriter w = new StreamWriter(Environment.CurrentDirectory + "\\result.xml", false, System.Text.Encoding.Default);
s.Serialize(w, res);
w.Close();

Works fine i get this:

<?xml version="1.0" encoding="Windows-1252"?>
<Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <htmlValue><b>Hello World</b></htmlValue>
</Result>

What can do i have to change to get this:

<?xml version="1.0" encoding="Windows-1252"?>
<Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <htmlValue><![CDATA[<b>Hello World</b>]]></htmlValue>
</Result>

I've already searched but I can't find anything. The type of htmlValue
have to stay String, because of other Serialisations JSON, etc.

Tricky one... Thanks in advance for suggestions

  • HTML is correct in String within C#. Why decode or encode?
  • XmlSerializer saved the HTML escaped to XML file.
  • Don't use C# for consuming.

Is external tool which accept this:

<htmlValue><![CDATA[<b>Hello World</b>]]></htmlValue>

but not

<htmlValue><b>Hello World</b></htmlValue>

I do the same with JSON Serializer, in file on hard drive the HTML is saved correct.
Why and where to use HTTP Utility to prevent that? And how to get <![CDATA[ ]]> around it.

Can you give a code sample?
Are there any other Serializer than the C# own one?

I've found this Link .NET XML Serialization of CDATA ATTRIBUTE from Marco André Silva, which does I need to do, but it's different, how to include this without changing Types?

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

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

发布评论

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

评论(3

爱她像谁 2024-08-13 22:13:15

这是一个实现您想要的目标的简单技巧。您只需要序列化 ​​XmlCDataSection 属性而不是字符串属性:(

这与 John 的建议几乎相同,但更简单一点......)

public class Result
{
    [XmlIgnore]
    public String htmlValue
    {
        get;
        set;
    }

    private static XmlDocument _dummyDoc;

    [XmlElement("htmlValue")]
    public XmlCDataSection htmlValueCData
    {
        get { return _dummyDoc.CreateCDataSection(htmlValue); }
        set { htmlValue = (value != null) ? value.Data : null; }
    }
}

Here's a simple trick to do achieve what you want. You just need to serialize a XmlCDataSection property instead of the string property :

(it's almost the same as John's suggestion, but a bit simpler...)

public class Result
{
    [XmlIgnore]
    public String htmlValue
    {
        get;
        set;
    }

    private static XmlDocument _dummyDoc;

    [XmlElement("htmlValue")]
    public XmlCDataSection htmlValueCData
    {
        get { return _dummyDoc.CreateCDataSection(htmlValue); }
        set { htmlValue = (value != null) ? value.Data : null; }
    }
}
郁金香雨 2024-08-13 22:13:15

请参阅“使用 XMLSerializer 进行 CDATA 序列化”对于同样的问题以及解决方案。

顺便说一句,在我看来,如果供应商不再存在,那么就该使用不同的产品了。可能是了解仅存在了十多年的 XML 规范的人。

See "CDATA serialization with XMLSerializer" for the same problem, and for the solution.

BTW, it seems to me that if the vendor no longer exists, it's time to use a different product. Possibly one that understands the XML specifications which have only existed for over a decade.

郁金香雨 2024-08-13 22:13:15

据我了解,您需要 XML 将其提供给某些实用程序。您是否还计划使用它来反序列化对象?

如果不是,那么为什么不自己做 - 序列化你的对象呢?往返对象 -> XML-> object 有点棘手,但第一部分则不然。

It is my understanding that you need the XML to feed it to some utility. Do you also plan to use it to de-serialize the object?

If not then why do not do it yourself - serialize your object that is? Roundtrip object -> XML -> object is somewhat tricky, but the first part is not.

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