XML转换为动态C#对象 [英] Converting XML to a dynamic C# object

查看:294
本文介绍了XML转换为动态C#对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的C#code到JSON数据字符串转换为使用JSON.Net框架动态对象:

I've used the following C# code to convert a string of JSON data to a dynamic object using the JSON.Net framework:

// Creates a dynamic .Net object representing the JSON data
var ProductDB = JsonConvert.DeserializeObject<dynamic>(JsonData);

转换后,我就可以直接使用code这样的访问内容:

Once converted, I can access the elements directly using code like this:

// Variables to be used
string ProductID;
string ProductType;
int ProductQty;

// Loop through each of the products
foreach (dynamic product in ProductDB.products)
{
    ProductID = product.id;
    ProductType = product.type;
    ProductQty = product.qty;
}

有什么类似这样的处理XML数据的?我可以只使用JSON.net到我的XML转换成JSON,然后重新使用code以上,但感觉就像作弊。

Is there anything similar to this for working with XML data? I could just use JSON.net to convert my XML to JSON and then re-use the code above, but that feels like cheating.

感谢。

推荐答案

我发现可按照您的要求一些好的解决方案:

I have found some good solutions available as per your requirement:


  1. 解析XML在C#动态对象

  2. Creating从XML使用ExpandoObject
  3. 动态对象
  4. Fluent XML解析使用C#的动态类型第1部分

  5. Dynamic用C#和.NET 4.0 XML阅读器

  1. Parse XML to dynamic object in C#
  2. Creating a dynamic object from XML using ExpandoObject
  3. Fluent XML Parsing Using C#'s Dynamic Type Part 1
  4. Dynamic XML Reader with C# and .Net 4.0

希望这些可以帮助你!

这篇关于XML转换为动态C#对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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