gpt4 book ai didi

c# - 使用 Dynamic 将 XML 反序列化为对象

转载 作者:IT王子 更新时间:2023-10-29 03:52:33 26 4
gpt4 key购买 nike

是否可以将未知的 XML 反序列化为如下对象?

 var xml = @"<Students><Student><Name>Arul</Name><Mark>90</Mark></Student></Students>";

var serializer = new XmlSerializer(typeof(DynamicObject));

dynamic students = serializer.Deserialize(new XmlTextReader(new StringReader(xml)));

最佳答案

你可能想试试这个。

string xml = @"<Students>
<Student ID=""100"">
<Name>Arul</Name>
<Mark>90</Mark>
</Student>
<Student>
<Name>Arul2</Name>
<Mark>80</Mark>
</Student>
</Students>";

dynamic students = DynamicXml.Parse(xml);

var id = students.Student[0].ID;
var name1 = students.Student[1].Name;

foreach(var std in students.Student)
{
Console.WriteLine(std.Mark);
}

public class DynamicXml : DynamicObject
{
XElement _root;
private DynamicXml(XElement root)
{
_root = root;
}

public static DynamicXml Parse(string xmlString)
{
return new DynamicXml(XDocument.Parse(xmlString).Root);
}

public static DynamicXml Load(string filename)
{
return new DynamicXml(XDocument.Load(filename).Root);
}

public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = null;

var att = _root.Attribute(binder.Name);
if (att != null)
{
result = att.Value;
return true;
}

var nodes = _root.Elements(binder.Name);
if (nodes.Count() > 1)
{
result = nodes.Select(n => n.HasElements ? (object)new DynamicXml(n) : n.Value).ToList();
return true;
}

var node = _root.Element(binder.Name);
if (node != null)
{
result = node.HasElements || node.HasAttributes ? (object)new DynamicXml(node) : node.Value;
return true;
}

return true;
}
}

--编辑--

为了使其与 xml namespace 一起使用,我添加了 RemoveNamespaces 方法。

public class DynamicXml : DynamicObject
{
XElement _root;
private DynamicXml(XElement root)
{
_root = root;
}

public static DynamicXml Parse(string xmlString)
{
return new DynamicXml(RemoveNamespaces(XDocument.Parse(xmlString).Root));
}

public static DynamicXml Load(string filename)
{
return new DynamicXml(RemoveNamespaces(XDocument.Load(filename).Root));
}

private static XElement RemoveNamespaces(XElement xElem)
{
var attrs = xElem.Attributes()
.Where(a => !a.IsNamespaceDeclaration)
.Select(a => new XAttribute(a.Name.LocalName, a.Value))
.ToList();

if (!xElem.HasElements)
{
XElement xElement = new XElement(xElem.Name.LocalName, attrs);
xElement.Value = xElem.Value;
return xElement;
}

var newXElem = new XElement(xElem.Name.LocalName, xElem.Elements().Select(e => RemoveNamespaces(e)));
newXElem.Add(attrs);
return newXElem;
}

public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = null;

var att = _root.Attribute(binder.Name);
if (att != null)
{
result = att.Value;
return true;
}

var nodes = _root.Elements(binder.Name);
if (nodes.Count() > 1)
{
result = nodes.Select(n => n.HasElements ? (object)new DynamicXml(n) : n.Value).ToList();
return true;
}

var node = _root.Element(binder.Name);
if (node != null)
{
result = node.HasElements || node.HasAttributes ? (object)new DynamicXml(node) : node.Value;
return true;
}

return true;
}
}

关于c# - 使用 Dynamic 将 XML 反序列化为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13704752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com