XML 反序列化和 XPath? [英] XML Deserialisation and XPath?

查看:24
本文介绍了XML 反序列化和 XPath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的 Xml

            <pda:Party>
                 ...Snip....
                <pda:CitizenName>
                    <pda:CitizenNameTitle>MR</pda:CitizenNameTitle>
                    <pda:CitizenNameForename>John</pda:CitizenNameForename>
                    <pda:CitizenNameSurname>Wayne</pda:CitizenNameSurname>
                </pda:CitizenName>
              .....Snip...
           </pda:Party>

Citizen Name 是派对节点中的复杂类型.(这是从我正在为其创建适配器的第 3 方集成收到的 xml)

Where Citizen Name is a complex type within the Party Node. ( This is xml received from a 3rd party integration that I'm creating an adapter for )

我对在我的班级中尝试反序列化为我宁愿拥有的子类型不感兴趣.

I'm not interested that there is a sub type as in my class I'm attempting to deserialize into I would rather have.

public class Party
{
    public string  FirstName { get; set; }
    public string LastName {get;set;}

}

因此,与其将我的类定义作为 XML 所代表内容的具体定义,我还可以使用诸如 XPath 之类的东西来装饰属性.

So rather than have my class definition as a concrete definition of what the XML represents, can I decorate the properties with something like XPath, eg.

 [XmlElement("\CitizenName\CitizenNameForeName")]
 public string FirstName {get;set;}

要从 xml 中挑选信息到包含我感兴趣的数据的类中?

To cherry pick information from the xml into a class the contains the data i'm interested in?

从3rd 方收到的xml 很冗长,我只对特定方面感兴趣.一种选择是只创建一个 XMLDocument 并使用 XPath 和转换方法手动映射到我的类,但我想我会问是否有中间解决方案?

The xml received from the 3rd party is very verbose and I'm only interested in specific aspects. One option is to just create an XMLDocument and map to my class manually using XPath and a conversion method, but I thought I would ask in case there was an intermediary solution?

推荐答案

最后,我设置了自己的属性来做我想让它做的事情.所以一个采用 XPath 路径的自定义属性...

In the end, I set up my own attribute to do what i wanted it to do. So a custom attribute that takes an XPath path...

[System.AttributeUsage(System.AttributeTargets.Property)]
public class PathToXmlNode : System.Attribute
{
    public string Path { get; set; }

    public PathToXmlNode(string path)
    {
        this.Path = path;
    }
}

后跟装饰属性..(为简单起见省略了命名空间)

followed by a decorated property.. ( namespaces omitted for simplicity )

         [PathToXmlNode("Party[1]/CitizenName/CitizenNameForename")]
         public string FirstName { get; set; }

然后当我想填充类时,我调用了以下方法.

Then when i want to populate the class I called the following method.

        var type = typeof(T);
        foreach (var property in type.GetProperties())
        {
            var attributes = property.GetCustomAttributes(typeof(PathToXmlNode), true);

            if (attributes != null && attributes.Length > 0)
            {
                //this property has this attribute assigned.
                //get the value to assign
                var xmlAttribute = (PathToXmlNode)attributes[0];
                var node = doc.SelectSingleNode(xmlAttribute.Path, nmgr);


                if (node != null && !string.IsNullOrWhiteSpace(node.InnerText))
                {
                    dynamic castedValue;

                    if (property.PropertyType == typeof(bool))
                    {
                        castedValue = Convert.ToBoolean(node.InnerText);
                    }
                    ...Snip all the casts....
                    else
                    {
                        castedValue = node.InnerText;
                    }


                    //we now have the node and it's value, now set it to the property.
                    property.SetValue(obj, castedValue, System.Reflection.BindingFlags.SetProperty, null, null, System.Globalization.CultureInfo.CurrentCulture);
                }

            }
        }

这是一个很好的起点,但是如果其他人认为这是一个可行的中间解决方案,您需要意识到它需要适应非简单数据类型.这就是我现在要做的事情!

This has been a good starting point, however If anyone else see's this as a viable intermediary solution, You need to be aware that it will need adapting for non simple data types. Which is what I'm setting off to do now!

这篇关于XML 反序列化和 XPath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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