基于属性将元素反序列化为属性 [英] Deserialize elements to properties based on attribute

查看:43
本文介绍了基于属性将元素反序列化为属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看是否有一种简单的方法可以将充满元素的 XML 文件反序列化回属性类,特别是将元素属性名称"作为要附加到的属性.例如,我有一个 XML 文件格式:

I am looking to see if there is a simple way to deserialize an XML file filled with elements back to a class of properties, specifically on the elements attribute 'name' as the property to attach to. For example, I have an XML file format given as:

<?xml version="1.0" encoding="utf-8"?>
<settings>
  <setting name="number_val_one">1</setting>
  <setting name="string_val_one">test</setting>
  <setting name="number_val_two">42</setting>
  <setting name="string_val_two">hello world</setting>
</settings>

我想知道是否有一种简单的方法可以将其反序列化回类似于这种格式的类:

I am wondering if there is a simple way to deserialize this back to a class that is similar to this format:

public class SomeObject
{
    [XmlElement("number_value_one")]
    public Int32 NumberValueOne { get; set; }

    [XmlElement("number_value_two")]
    public Int32 NumberValueTwo { get; set; }

    [XmlElement("string_value_one")]
    public String StringValueOne { get; set; }

    [XmlElement("string_value_two")]
    public String StringValueTwo { get; set; }
}

我知道该格式不是序列化的最佳格式,但我无法更改此项目的文件格式.目前,我正在手动读取每个元素的价值,这是大量代码且相当混乱.为了将它序列化回 xml,我使用反射来解决手动编写每个元素的需要.但我想知道是否有一种简单的方法来处理这种格式.

I understand the format is no the best for serialization and such but I cannot change how the file is formatted for this project. At the moment I am reading each element by hand for its value which is a lot of code and fairly messy. To serialize it back to xml I am using reflection to get around the need to write each element by hand. But I am wondering if there is a simple way to handle this format.

虽然并非所有元素都是字符串/整数,但有些是在读取后要解析的自定义类型等.

Not all elements are string / int though, some are custom types to be parsed after being read etc.

推荐答案

对于其中之一,您可以使用 xslt 将输入的字符串转换为 XmlSerializer 可识别的数据.

For one you can use xslt to transform you input string to data recognizable by XmlSerializer.

var transform = new XslCompiledTransform();
transform.Load(XmlReader.Create(new StringReader(transformText)));
var memoryStream = new MemoryStream();
transform.Transform(new XPathDocument(new StringReader(text)),null,memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
var deserializer = new XmlSerializer(typeof(SomeObject)).Deserialize(memoryStream);

哪里

            var transformText = @"
<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"">
    <xsl:output method=""xml"" encoding=""utf-8"" indent=""no""/>
    <xsl:template match=""/"">
        <SomeObject>
            <xsl:for-each select=""settings/setting"">
                <xsl:element name=""{@name}"">
                    <xsl:value-of select="".""/>
                </xsl:element>
            </xsl:for-each>
        </SomeObject>   
    </xsl:template>
</xsl:stylesheet>";

        var text = @"
<settings>
    <setting name=""number_val_one"">1</setting>
    <setting name=""string_val_one"">test</setting>
    <setting name=""number_val_two"">42</setting>
    <setting name=""string_val_two"">hello world</setting>
</settings>";

注意setting name 属性值必须与应用于属性的XmlElementAttribute 匹配.

note that setting name attribute value must mach with XmlElementAttribute applied to property.

如果您不了解 xslt,只需搜索它就有很多示例和 参考 在那里.

If you don't understand xslt just search for it there is a lot of examples and references out there.

这篇关于基于属性将元素反序列化为属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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