相关疑难解决方法(0)

将XML转换为动态C#对象

我使用以下C#代码使用JSON.Net框架将一串JSON数据转换为动态对象:

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

转换后,我可以使用以下代码直接访问元素:

// 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;
}
Run Code Online (Sandbox Code Playgroud)

使用XML数据有什么类似的东西吗?我可以使用JSON.net将我的XML转换为JSON,然后重新使用上面的代码,但这就像是作弊.

谢谢.

c# xml json

56
推荐指数
3
解决办法
7万
查看次数

标签 统计

c# ×1

json ×1

xml ×1