Csharp/C#教程:XML反序列化和XPath?分享


XML反序列化和XPath?

我有这样的Xml

 ...Snip....  MR John Wayne  .....Snip...  

其中Citizen Name是Party Node中的复杂类型。 (这是从我正在创建适配器的第三方集成中收到的xml)

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

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

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

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

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

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

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

最后,我设置了自己的属性来做我想做的事情。 所以采用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); } } } 

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

上述就是C#学习教程:XML反序列化和XPath?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ctvol.com/cdevelopment/1008594.html

(0)
上一篇 2021年12月29日
下一篇 2021年12月29日

精彩推荐