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:
- Parse XML to dynamic object in C#
- Creating a dynamic object from XML using ExpandoObject
- Fluent XML Parsing Using C#'s Dynamic Type Part 1
- Dynamic XML Reader with C# and .Net 4.0
希望这些可以帮助你!