如何使用 C# 分析深层 XML 值

本文关键字:XML 何使用 | 更新日期: 2023-09-27 18:30:23

Working in Visual Studio 2010, .NET v4

我是C#和XML的新手。使用网站资源,我能够找到一种相当简单的方法来解析特定值的XML。然而,XML变得越来越复杂,我使用的方法(一种"通过迭代导航"技术)开始看起来相当荒谬,有许多嵌套阅读器。读取() 调用。我确信有一种不那么尴尬的方式,但是当你拥有的唯一工具是锤子时......

所以,问题是:解析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;
}

如何使用 C# 分析深层 XML 值

var node = XDocument.Load(fname)
                    .XPathSelectElement("//item[@itemkind='Wanted']/phrase");
var text = node.Value;
var val = XDocument.Load(filename) // OR  XDocument.Parse(xmlstring)
            .Descendants("item")
            .First(i => i.Attribute("itemkind").Value == "Wanted")
            .Element("phrase")
            .Value;

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"));

由于您有一个定义的 XML 架构,因此我只需声明匹配的类来表示该数据,并使用 XmlSerializer 反序列化 XML。

因此,根据您发布的 XML,您的类可能如下所示:

[XmlType("formats")]
public class Formats
{
    [XmlArray("items")]
    public List<Item> Items { get; set; }
    [XmlArray("styles")]
    public List<Style> Styles { get; set; }
}
[XmlType("item")]
public class Item
{
    [XmlAttribute("itemkind")]
    public string ItemKind { get; set; }
    [XmlElement("phrase")]
    public string Phrase { get; set; }
    [XmlArray("actions")]
    public List<Action> Actions { get; set; }
}
[XmlType("action")]
public class Action
{
    [XmlElement("actionkind")]
    public string ActionKind { get; set; }
    [XmlElement("parameter")]
    public string Parameter { get; set; }
}
[XmlType("style")]
public class Style
{
    [XmlAttribute("stylename")]
    public string StyleName { get; set; }
    [XmlElement("fontname")]
    public string FontName { get; set; }
    [XmlElement("fontsize")]
    public int FontSize { get; set; }
    [XmlElement("bold")]
    public bool Bold { get; set; }
}

示例反序列化代码可能如下所示:

XmlSerializer mySerializer = new XmlSerializer(typeof(Formats));
FileStream myFileStream = new FileStream(AFileName, FileMode.Open);
var formats = (Formats)mySerializer.Deserialize(myFileStream);

然后,搜索数据非常简单,就像遍历对象及其属性一样:

List<Action> matchingActions = formats.Items
    .Where(item => item.ItemKind == "Wanted")
    .Where(item => item.Phrase == "SomeSearchString")
    .SelectMany(item => item.Actions)
    .ToList();

这样做的一个很好的方面是减少了对定义 XML 架构的硬编码字符串文本的依赖。快速重构或更改数据结构变得非常容易,而不会意外破坏应用程序。另请注意,它为您处理几种类型分析情况(例如,Style.Bold属性可以很容易地强类型化为bool,而不是解析为"0"或"1"string

另一种选择是使用 XML 序列化框架。这允许您来回映射 XML 和 C# 对象。对于某些应用程序,这非常有效。它允许您几乎以独占方式处理 C# 对象,而不必担心逐位分析 XML。

您可能想要查看 LINQToXML 的 MSDN 页面。例如,LINQ to XML。或者,只需搜索"LinQ to XML"。有很多有用的文章。