XML反序列化和XPath?

时间:2013-01-02 14:10:49

标签: c# xml serialization

我有像这样的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是Party Node中的复杂类型。 (这是从我正在创建适配器的第三方集成中收到的xml)

我不感兴趣,因为我的班级中有一个子类型,我试图反序列化,我宁愿拥有。

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

}

因此,我不是将我的类定义作为XML表示的具体定义,而是可以使用类似XPath的东西来修饰属性,例如。

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

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

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

2 个答案:

答案 0 :(得分:0)

一种选择是使用XSLT转换将传入的XML解析为与您的类匹配的格式。

答案 1 :(得分:0)

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

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

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

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

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

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

        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);
                }

            }
        }

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