在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>
Run Code Online (Sandbox Code Playgroud)
这是我到达的代码.它确实有效,但是,好吧,看看你自己.请温柔:
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;
}
Run Code Online (Sandbox Code Playgroud)
记住我是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;
}
Run Code Online (Sandbox Code Playgroud)
Linq to XML 就是您想要的...您还想要操作,对吧?我认为其他任何答案都没有给你这个......
var itemKind = "Wanted";
var searchPhrase = "SomeSearchString";
var doc = XDocument.Load(filename);
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"));
Run Code Online (Sandbox Code Playgroud)