代码之家  ›  专栏  ›  技术社区  ›  Nam G VU

如何提取xaml/xml文件的特定标记的所有出现?

  •  2
  • Nam G VU  · 技术社区  · 14 年前

    假设我需要使用C#提取字典资源文件中的所有实体画笔和线性渐变画笔。我该怎么做?请帮忙!

    这个问题可以扩展为更一般的问题,例如“如何在使用C#搜索文件时找到多行匹配项?”

    2 回复  |  直到 14 年前
        1
  •  1
  •   erlando    14 年前

    下面是一个使用Linq to XML的简单示例,可以帮助您开始:

    string xaml = @"
      <Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
            xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
            xmlns:basics=
             'clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls'>
          <Canvas x:Name='BackgroundCanvas' Width='1200' Height='500' 
                  Background='White'>
              <basics:Button x:Name='Cancel' Width='100' Height='40'  />
              <basics:Button x:Name='Save' Width='100' Height='40'  />
          </Canvas>
      </Grid>";
    
    XDocument doc = XDocument.Parse(xaml);
    
    // XAML uses lots of namespaces so set these up
    XNamespace def = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
    XNamespace basics = "clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls";
    XNamespace x = "http://schemas.microsoft.com/winfx/2006/xaml";
    
    // Find the button named "Cancel" on the canvas
    var button = doc.Elements(def + "Grid")
            .Elements(def + "Canvas")
            .Elements(basics + "Button")
            .Where(a => (string)a.Attribute(x + "Name") == "Cancel")
            .SingleOrDefault();
    
    if (button != null)
    {
        // Print the width attribute
        Console.WriteLine(button.Attribute("Width").Value);
    }
    

    有两种方法可以使用XPath,但首先我们需要设置 XmlNamespaceManager XNamespace 上课。以下两个示例都将使用此选项:

    XmlNamespaceManager nsm = new XmlNamespaceManager(new NameTable());
    nsm.AddNamespace("def", 
       "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
    nsm.AddNamespace("basics", 
       "clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls");
    nsm.AddNamespace("x", 
       "http://schemas.microsoft.com/winfx/2006/xaml");
    

    使用XPath查询XDocument,然后更改 Width “保存”按钮上的属性:

    XElement saveButtonXe = 
            ((IEnumerable)doc.
              XPathEvaluate("//def:Canvas/basics:Button[@x:Name = 'Save']", nsm))
              .Cast<XElement>()
              .SingleOrDefault();
    
    if(saveButtonXe != null)
    {
      // Set the Width value
      saveButtonXe.Attribute("Width").SetValue("250");
      Console.WriteLine(doc.ToString());
    }
    

    XmlDocument :

    // Remember to initialise the XmlNamespaceManager described above
    XmlDocument oldSkool = new XmlDocument();
    oldSkool.LoadXml(xaml);
    XmlNode saveButtonNode = 
       oldSkool.SelectSingleNode("//def:Canvas/basics:Button[@x:Name = 'Save']", nsm);
    if(saveButtonNode != null)
    {
      Console.WriteLine(saveButtonNode.Attributes["Width"].Value);
    }
    
        2
  •  0
  •   ridoy Ali Umair    11 年前

    将文件加载到 XDocument 用它来找到你的火柴。 Regex xml