[英]Converting XML to a dynamic C# object
I've used the following C# code to convert a string of JSON data to a dynamic object using the JSON.Net framework:我使用以下 C# 代码使用 JSON.Net 框架将一串 JSON 数据转换为动态对象:
// Creates a dynamic .Net object representing the JSON data
var ProductDB = JsonConvert.DeserializeObject<dynamic>(JsonData);
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;
}
Is there anything similar to this for working with XML data?是否有与此类似的用于处理 XML 数据的内容? I could just use JSON.net to convert my XML to JSON and then re-use the code above, but that feels like cheating.
我可以使用 JSON.net 将我的 XML 转换为 JSON,然后重新使用上面的代码,但这感觉像是在作弊。
Thanks.谢谢。
XDocument doc = XDocument.Parse(xmlData); //or XDocument.Load(path)
string jsonText = JsonConvert.SerializeXNode(doc);
dynamic dyn = JsonConvert.DeserializeObject<ExpandoObject>(jsonText);
I think "cheating" is the answer - the xml solutions are very long :)我认为“作弊”就是答案——xml 解决方案很长:)
An alternative for future visitors, the one from ITDevSpace doesn't include attributes on elements with children.未来访问者的另一种选择,来自 ITDevSpace 的不包括具有子元素的元素的属性。
public class XmlWrapper
{
public static dynamic Convert(XElement parent)
{
dynamic output = new ExpandoObject();
output.Name = parent.Name.LocalName;
output.Value = parent.Value;
output.HasAttributes = parent.HasAttributes;
if (parent.HasAttributes)
{
output.Attributes = new List<KeyValuePair<string, string>>();
foreach (XAttribute attr in parent.Attributes())
{
KeyValuePair<string, string> temp = new KeyValuePair<string, string>(attr.Name.LocalName, attr.Value);
output.Attributes.Add(temp);
}
}
output.HasElements = parent.HasElements;
if (parent.HasElements)
{
output.Elements = new List<dynamic>();
foreach (XElement element in parent.Elements())
{
dynamic temp = Convert(element);
output.Elements.Add(temp);
}
}
return output;
}
}
Cinchoo ETL - an open source library available to parse xml into dynamic object Cinchoo ETL - 可用于将 xml 解析为动态对象的开源库
using (var p = ChoXmlReader.LoadText(xml).WithXPath("/"))
{
foreach (dynamic rec in p)
Console.WriteLine(rec.Dump());
}
Checkout CodeProject article for some additional help.查看 CodeProject 文章以获得一些额外的帮助。
Disclaimer: I'm the author of this library.免责声明:我是这个库的作者。
From @FSX's answer I have successfully used the solution from " Parse XML to dynamic object in C# ":从@FSX 的回答中,我成功地使用了从“ 解析 XML 到 C# 中的动态对象”的解决方案:
public class XmlToDynamic
{
public static void Parse(dynamic parent, XElement node)
{
if (node.HasElements)
{
if (node.Elements(node.Elements().First().Name.LocalName).Count() > 1)
{
//list
var item = new ExpandoObject();
var list = new List<dynamic>();
foreach (var element in node.Elements())
{
Parse(list, element);
}
AddProperty(item, node.Elements().First().Name.LocalName, list);
AddProperty(parent, node.Name.ToString(), item);
}
else
{
var item = new ExpandoObject();
foreach (var attribute in node.Attributes())
{
AddProperty(item, attribute.Name.ToString(), attribute.Value.Trim());
}
//element
foreach (var element in node.Elements())
{
Parse(item, element);
}
AddProperty(parent, node.Name.ToString(), item);
}
}
else
{
AddProperty(parent, node.Name.ToString(), node.Value.Trim());
}
}
private static void AddProperty(dynamic parent, string name, object value)
{
if (parent is List<dynamic>)
{
(parent as List<dynamic>).Add(value);
}
else
{
(parent as IDictionary<String, object>)[name] = value;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.