C# 使用c从xml文件中读取动态元素#`
我想从xml文件中动态读取xml元素(我的意思是不需要硬编码元素名称)。我无法使用XmlReader.ReadToDescendant方法,因为它需要一个本地名称,在我的情况下,这个名称会有所不同。例如,在本例中,我需要读取元素A、B、C、D等C# 使用c从xml文件中读取动态元素#`,c#,xml,C#,Xml,我想从xml文件中动态读取xml元素(我的意思是不需要硬编码元素名称)。我无法使用XmlReader.ReadToDescendant方法,因为它需要一个本地名称,在我的情况下,这个名称会有所不同。例如,在本例中,我需要读取元素A、B、C、D等 <?xml version="1.0" encoding="UTF-8"?> <test Version="2010" xmlns="http://test.org/2010/values"> <A> <
<?xml version="1.0" encoding="UTF-8"?>
<test Version="2010" xmlns="http://test.org/2010/values">
<A>
<Data>
<Somedata></Somedata>
</Data>
<Rows>
<Row></Row>
<Row></Row>
</Rows>
</A>
<B>
<Data>
<Somedata></Somedata>
</Data>
<Rows>
<Row></Row>
<Row></Row>
</Rows>
</B>
<C>
<Data>
<Somedata></Somedata>
</Data>
<Rows>
<Row></Row>
<Row></Row>
</Rows>
</C>
<D>
<Data>
<Somedata></Somedata>
</Data>
<Rows>
<Row></Row>
<Row></Row>
</Rows>
</D>
</test>
请帮帮我。这很简单:
XDocument doc = XDocument.Load("test.xml");
string name = GetNameFromWherever();
foreach (XElement match in doc.Descendants(name))
{
...
}
这就是使用LINQtoXML——如果您使用的是.NET3.5或更高版本,这是一个可爱的XML API。。。它比使用XmlReader
好得多,这非常简单:
XDocument doc = XDocument.Load("test.xml");
string name = GetNameFromWherever();
foreach (XElement match in doc.Descendants(name))
{
...
}
这就是使用LINQtoXML——如果您使用的是.NET3.5或更高版本,这是一个可爱的XML API。。。它比使用
XmlReader
将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; }
}
要将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; }
}
那你打算在哪里读这些名字呢?从另一个文件或数据库?那么你要从哪里读取这些名称?来自其他文件或数据库?感谢您的回复。如果我不知道元素,有没有办法从XmlDocument动态获取数据?@user972255:不清楚您的意思-这是基于一个可以在执行时获取的名称获取元素,例如,通过询问用户。Ok。如果您有多个名称,那么我们需要在子体循环的顶部有另一个循环来逐个获取名称?@user972255:这是最简单的方法,是的-或者您可以使用
doc.subjections().Where(x=>names.Contains(x.name.LocalName))
。感谢您的回复。如果我不知道元素,有没有办法从XmlDocument动态获取数据?@user972255:不清楚您的意思-这是基于一个可以在执行时获取的名称获取元素,例如,通过询问用户。Ok。如果您有多个名称,那么我们需要在子体循环的顶部有另一个循环来逐个获取名称?@user972255:这是最简单的方法,是的-或者您可以使用doc.subjections().Where(x=>names.Contains(x.name.LocalName))
。