0
我有一个稍大的XML文件(下面发布),我正在尝试使用C#读取。我已经知道它会读取XML文件中的名称和类型(我将在稍后使用它),但是我想在找到某个标签停止的时候找到一种方法。C#尝试读取XML并在某个点停止
例如:
我现在寻找到的数据属性物理ATTR,然后循环让所有的名称和类型的值。
我希望它在到达negativePhysical时停下来,因为那是其他信息。
这是我一直在使用thusfar代码:
XmlTextReader reader = new XmlTextReader("/Attributes.xml");
while (reader.Read())
{
reader.ReadToFollowing("data");
reader.ReadToFollowing("attributes");
reader.ReadToFollowing("physical");
reader.ReadToFollowing("attr");
while (reader.Read())
{
reader.ReadToFollowing("name");
name = reader.ReadString();
reader.ReadToFollowing("type");
type = reader.ReadString();
}
}
这里是XML的一个小样本,我使用:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<attributes>
<physical>
<attr>
<name>Aggressive</name>
<type>Unk</type>
</attr>
<attr>
<name>Agile</name>
<type>Msc</type>
</attr>
</physical>
<negativePhysical>
<attr>
<name>Clumsy</name>
</attr>
<attr>
<name>Cowardly</name>
</attr>
</negativePhysical>
<social>
<attr>
<name>Alluring</name>
<type></type>
</attr>
<attr>
<name>Beguiling</name>
<type></type>
</attr>
</social>
<negativeSocial>
<attr>
<name>Bestial</name>
</attr>
<attr>
<name>Callous</name>
</attr>
</negativeSocial>
<mental>
<attr>
<name>Alert</name>
<type></type>
</attr>
<attr>
<name>Analytical</name>
<type></type>
</attr>
</mental>
<negativeMental>
<attr>
<name>Deceitful</name>
</attr>
<attr>
<name>Forgetful</name>
</attr>
</negativeMental>
</attributes>
</data>
最终,每个“属性”,将迭代完成后,我将在稍后将更多信息添加到“能力”文件中。我已经研究过这个代码,试图在此上使用LINQ,但到目前为止,我已经无法得到任何返回的东西。
我试过了。当我放入'XmlDocument doc = new XmlDocument.LoadXml(“/ Attributes.xml”);'它给了我错误('System.Xml.XmlDocument.LoadXml(string)'是'方法',但像使用'type'),而我一直无法弄清楚。 –
'LoadXml'从'string'中读取XML。改为尝试'加载'。它从文件中读取。 –
我刚刚在另一个帖子里发现了,谢谢。我会玩这个,看看我能想出什么。 –