C# 将内部 XML 反序列化为字符串 [英] C# Deserialize inner XML to string

查看:64
本文介绍了C# 将内部 XML 反序列化为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XML:

<MyProperty1>值 1</MyProperty1><MyProperty2>值2</MyProperty2><MyNestedXml><h1>标题</h1><p>Lorum</p><p>Ipsum</p></MyNestedXml></MyType>

这可以通过创建类并添加如下属性在 C# 中反序列化为 XML:

[可序列化]公共类 MyType{[XmlElement("MyProperty1")公共字符串 MyProperty1 { 获取;放;}[XmlElement("MyProperty2")公共字符串 MyProperty2 { 获取;放;}[XmlIgnore]公共字符串 MyNestedXml { 获取;放;}}

但是,<MyNestedXml> 元素中的内部 XML 变化并且不遵循我可以使用属性有效映射的一致结构.

不幸的是,我无法控制 XML 结构.

我尝试将 [XmlElement("MyNestedXml") 与 XmlNode 类型一起使用,但这会导致反序列化第一个子节点而不是整个内部 XML.

我也尝试反序列化为一种字符串,但会引发 InvalidOperationException:

意外的节点类型元素.ReadElementString 方法只能在内容简单或内容为空的元素上调用."

问题是 MyNestedXml 元素的内容有时可能是一个 Elements 数组,但有时可能是简单的或空的内容.

理想情况下,我可以使用不同的序列化属性(例如 [XmlAsString])来完全跳过序列化并按原样分配内部 XML.

预期的结果将是一个 MyType 类型的类,它具有属性 MyProperty1 = "Value 1"、属性 MyProperty2 = "Value 2" 和属性 MyNestedXml= "

Heading

Lorum

Ipsum

".

解决方案

使用 XmlAnyElement 属性使其成为 XmlElement 属性,序列化程序将反序列化任意 XML 结构.

[XmlRoot("MyType")]公共类 MyType{[XmlElement("MyProperty1")]公共字符串 MyProperty1 { 获取;放;}[XmlElement("MyProperty2")]公共字符串 MyProperty2 { 获取;放;}[XmlAnyElement("MyNestedXml")]公共 XmlElement MyNestedXml { 获取;放;}}

I have the following XML:

<MyType>
  <MyProperty1>Value 1</MyProperty1>
  <MyProperty2>Value 2</MyProperty2>
  <MyNestedXml>
    <h1>Heading</h1>
    <p>Lorum</p>
    <p>Ipsum</p>
  </MyNestedXml>
</MyType>

This can be deserialized to XML in C# by creating classes and adding attributes as below:

[Serializable]
public class MyType
{
    [XmlElement("MyProperty1")
    public string MyProperty1 { get; set; }

    [XmlElement("MyProperty2")
    public string MyProperty2 { get; set; }

    [XmlIgnore]
    public string MyNestedXml { get; set; }
}

However, the inner XML within the <MyNestedXml> element varies and doesn't follow a consistent structure which I can effectively map using attributes.

I don't have control over the XML structure unfortunately.

I have tried using an [XmlElement("MyNestedXml") with an XmlNode type but this results in the first child node being deserialized instead of the entire inner XML.

I have also tried deserializing to a type of string but that throws an InvalidOperationException:

"Unexpected node type Element. ReadElementString method can only be called on elements with simple or empty content."

The problem is that the content of the MyNestedXml element could be an array of Elements sometimes but could be simple or empty content at other times.

Ideally I could use a different serialization attribute such as [XmlAsString] to skip serialization altogether and just assign the inner XML as is.

The intended result would be a class of type MyType having a property MyProperty1 = "Value 1", a property MyProperty2 = "Value 2", and a property MyNestedXml = "<h1>Heading</h1><p>Lorum</p><p>Ipsum</p>".

解决方案

Make it an XmlElement property with the XmlAnyElement attribute and the serializer will deserialize an arbitrary XML structure.

[XmlRoot("MyType")]
public class MyType
{
    [XmlElement("MyProperty1")]
    public string MyProperty1 { get; set; }

    [XmlElement("MyProperty2")]
    public string MyProperty2 { get; set; }

    [XmlAnyElement("MyNestedXml")]
    public XmlElement MyNestedXml { get; set; }
}

这篇关于C# 将内部 XML 反序列化为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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