XML反序列化和XPath?

Chr*_*ris 5 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>
Run Code Online (Sandbox Code Playgroud)

其中“公民姓名”是“聚会”节点内的复杂类型。(这是我正在为其创建适配器的第三方集成提供的xml)

我对在我想反序列化为类的类中没有子类型不感兴趣。

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

}
Run Code Online (Sandbox Code Playgroud)

因此,除了将我的类定义作为XML表示的具体定义之外,我还可以使用XPath之类的东西来装饰属性。

 [XmlElement("\CitizenName\CitizenNameForeName")]
 public string FirstName {get;set;}
Run Code Online (Sandbox Code Playgroud)

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

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

Chr*_*ris 0

最后,我设置了自己的属性来执行我想要它执行的操作。因此,采用 XPath 路径的自定义属性...

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

    public PathToXmlNode(string path)
    {
        this.Path = path;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

         [PathToXmlNode("Party[1]/CitizenName/CitizenNameForename")]
         public string FirstName { get; set; }
Run Code Online (Sandbox Code Playgroud)

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

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

            }
        }
Run Code Online (Sandbox Code Playgroud)

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