在 Visual Studio 2010 中工作,.NET v4
我是 C# 和 XML 的相对新手。使用网站资源,我能够制定出一种相当简单的方法来解析特定值的 XML。然而,XML 变得越来越复杂,我使用的方法(有点像“通过迭代导航”技术)开始看起来相当荒谬,有许多嵌套的 reader.Read() 调用。我敢肯定有一种不那么尴尬的方法,但是当你唯一的工具是锤子时......
所以,问题是:什么是解析 XML 的简洁、干净的方法(见下文),仅从匹配的项目中返回所有“操作”值的列表<item itemkind="Wanted">
和<phrase>SomeSearchString</phrase>
?
这是 XML 的一个片段:
<formats>
<items>
<item itemkind="NotWanted">
<phrase>Not this one</phrase>
<actions>
<action>
<actionkind>SetStyle</actionkind>
<parameter>Normal</parameter>
</action>
<action>
<actionkind>SetMargins</actionkind>
<parameter>0.25,0.25,1,4</parameter>
</action>
</actions>
</item>
<item itemkind="Wanted">
<phrase>SomeSearchString</phrase>
<actions>
<action>
<actionkind>Action 1</actionkind>
<parameter>Param 1</parameter>
</action>
<action>
<actionkind>Action 2</actionkind>
<parameter>Param 2</parameter>
</action>
<action>
<actionkind>Action 3</actionkind>
<parameter>Param 3</parameter>
</action>
</actions>
</item>
</items>
<styles>
<style stylename="Normal">
<fontname>Arial</fontname>
<fontsize>10</fontsize>
<bold>0</bold>
</style>
<style stylename="Heading">
<fontname>fntame frhead</fontname>
<fontsize>12</fontsize>
<bold>1</bold>
</style>
</styles>
</formats>
这是我得到的代码。它确实有效,但是,你自己看看。请温柔:
public static List<TAction> GetActionsForPhraseItem(string AFileName, string APhrase)
{
List<TAction> list = new List<TAction>();
string xmlactionkind = null;
string xmlparameter = null;
string match = null;
// Search through XML items
using (XmlReader reader = XmlReader.Create(AFileName))
{
if (reader.ReadToFollowing("items"))
{
while (reader.Read())
{
if (reader.ReadToFollowing("item"))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && reader.GetAttribute("itemkind") == "Phrase")
{
if (reader.ReadToFollowing("phrase"))
{
match = reader.ReadString();
if (match == APhrase)
{
if (reader.ReadToFollowing("actions"))
{
// Use a subtree to deal with just the aItemKind item actions
using (var SubTree = reader.ReadSubtree())
{
bool HaveActionKind = false;
bool HaveParameter = false;
while (SubTree.Read())
{
if (SubTree.NodeType == XmlNodeType.Element && SubTree.Name == "actionkind")
{
xmlactionkind = SubTree.ReadString();
HaveActionKind = true;
}
if (SubTree.NodeType == XmlNodeType.Element && SubTree.Name == "parameter")
{
xmlparameter = SubTree.ReadString();
HaveParameter = true;
}
if ((HaveActionKind == true) && (HaveParameter == true))
{
TAction action = new TAction()
{
ActionKind = xmlactionkind,
Parameter = xmlparameter
};
list.Add(action);
HaveActionKind = false;
HaveParameter = false;
}
}
}
}
}
}
}
}
}
}
}
}
return list;
}
考虑到我是 C# 的新手,我怀疑 LINQ 在这里会非常有用,但到目前为止,我还无法完全理解它。我想,一次尝试学习太多新事物。提前感谢您的任何帮助(和建设性的批评)。
编辑:这是我最终得到的最终工作代码。感谢所有回复的人!
public static List<TAction> GetActionsForPhraseItemTWO(string AFileName, string ASearchPhrase)
{
List<TAction> list = new List<TAction>();
var itemKind = "Wanted";
var searchPhrase = ASearchPhrase;
var doc = XDocument.Load(AFileName);
var matches = doc.Descendants("item")
.Where(x => x.Attribute("itemkind") != null &&
x.Attribute("itemkind").Value == itemKind &&
x.Descendants("phrase").FirstOrDefault() != null &&
x.Descendants("phrase").FirstOrDefault().Value == searchPhrase)
.SelectMany(x => x.Descendants("action"));
foreach (var temp in matches)
{
TAction action = new TAction()
{
ActionKind = temp.Element("actionkind").Value.ToString(),
Parameter = temp.Element("parameter").Value.ToString()
};
list.Add(action);
}
return list;
}