C# 在不知道结构的情况下,以通用方式读取xml文件/字符串
我想将XML层次结构读入内存中对象的树中。XML树可以有n个级别的子级。我不知道确切的数字。我的内存中对象具有要绑定到树控件的子级和父级属性 当我不知道如何准确地调用/写入xml元素标记时,如何以通用方式将xml文件/字符串读入内存中的对象 例如,有人可以为我提供单元的xml结构,其中每个单元都有许多单元等等。。。所以我知道xml标记是“unit”,但也可以是“module”或其他任何东西。。。它必须是通用的,但我不知道如何要求用户输入xml元素标记名,比如“unit”C# 在不知道结构的情况下,以通用方式读取xml文件/字符串,c#,xml,linq,generics,C#,Xml,Linq,Generics,我想将XML层次结构读入内存中对象的树中。XML树可以有n个级别的子级。我不知道确切的数字。我的内存中对象具有要绑定到树控件的子级和父级属性 当我不知道如何准确地调用/写入xml元素标记时,如何以通用方式将xml文件/字符串读入内存中的对象 例如,有人可以为我提供单元的xml结构,其中每个单元都有许多单元等等。。。所以我知道xml标记是“unit”,但也可以是“module”或其他任何东西。。。它必须是通用的,但我不知道如何要求用户输入xml元素标记名,比如“unit” 这可能是我想要实现的吗?
这可能是我想要实现的吗?我只需将其加载到
XmlDocument
中,然后构建一棵树,遍历XmlNodes查看并查看包。本文也将帮助您:
像这样吗
using System.Xml ;
using System.IO;
class Program
{
static void Main( string[] args )
{
using ( Stream inputStream = OpenXmlStream() )
{
XmlDocument document = new XmlDocument() ;
document.Load( inputStream ) ;
Process( document ) ;
}
}
static Stream OpenXmlStream()
{
// provide an input stream for the program
}
static void Process( XmlDocument document )
{
// do something useful here
}
}
我相信有可能实现你想要实现的目标。我会这样做:
class GenericNode
{
private List<GenericNode> _Nodes = new List<GenericNode>();
private List<GenericKeyValue> _Attributes = new List<GenericKeyValue>();
public GenericNode(XElement Element)
{
this.Name = Element.Name;
this._Nodes.AddRange(Element.Elements()
.Select(e => New GenericNode(e));
this._Attributes.AddRange(
Element.Attributes()
.Select(a => New GenericKeyValue(a.Key, a.Value))
}
public string Name { get; private set; }
public IEnumerable<GenericNode> Nodes
{
get
{
return this._Nodes;
}
}
public IEnumerable<GenericKeyValue> Attributes
{
get
{
return this._Attributes;
}
}
}
class GenericKeyValue
{
public GenericKeyValue(string Key, string Value)
{
this.Key = Key;
this.Value = Value;
}
public string Key { get; set; }
public string Value { get; set; }
}
<module name="core">
<modules>
<module name="xml">
<modules>
<module name="reader" />
<module name="writer" />
</modules>
</module>
<module name="json">
<modules>
<module name="serializer" />
<module name="deserializer" />
</modules>
</module>
</modules>
</module>
XElement xElement = XElement.Load(@"path\to\your\xml\file");
string rootNodeName = xElement.Name.LocalName;
IEnumerable<XElement> xElements = xElement.Descendants(rootNodeName + "s");
无论哪种方式,您都必须知道节点名称才能解析层次结构,至少您必须有一个定义。总之,XElement
是唯一一种通用的XML
解析器。例如,您有这样一个XML:
class GenericNode
{
private List<GenericNode> _Nodes = new List<GenericNode>();
private List<GenericKeyValue> _Attributes = new List<GenericKeyValue>();
public GenericNode(XElement Element)
{
this.Name = Element.Name;
this._Nodes.AddRange(Element.Elements()
.Select(e => New GenericNode(e));
this._Attributes.AddRange(
Element.Attributes()
.Select(a => New GenericKeyValue(a.Key, a.Value))
}
public string Name { get; private set; }
public IEnumerable<GenericNode> Nodes
{
get
{
return this._Nodes;
}
}
public IEnumerable<GenericKeyValue> Attributes
{
get
{
return this._Attributes;
}
}
}
class GenericKeyValue
{
public GenericKeyValue(string Key, string Value)
{
this.Key = Key;
this.Value = Value;
}
public string Key { get; set; }
public string Value { get; set; }
}
<module name="core">
<modules>
<module name="xml">
<modules>
<module name="reader" />
<module name="writer" />
</modules>
</module>
<module name="json">
<modules>
<module name="serializer" />
<module name="deserializer" />
</modules>
</module>
</modules>
</module>
XElement xElement = XElement.Load(@"path\to\your\xml\file");
string rootNodeName = xElement.Name.LocalName;
IEnumerable<XElement> xElements = xElement.Descendants(rootNodeName + "s");
当然,您可以Linq
元素xElements
并解析层次结构,您可以重复构建树控件
您可以通过以下链接启动xElement:
希望这能有所帮助。@Jakuk如果我不知道客户的xml文件及其结构,那么仅仅读取每个节点并假设我所做的一切都是正确的就太疯狂了。。。最重要的是,我从哪里知道xml元素标记名称?@msfanboy-要么知道节点名称,只想读取它们,要么不知道它们,只想读取整个文件。没有别的办法了。您询问了当节点未知时如何以通用方式读取xml文件。如果你想要一个不同的答案,问一个不同的问题,你似乎在描述DOM的概念。您的具有子级和父级属性的通用内存中对象听起来与DOM节点完全一样。我的内存中对象是ViewModels,是treeview控件的数据源。我知道。您的代码看起来可以工作;-)我将遵循您的建议:rootNodeName+“s”);我想现在对我来说没关系了!