c# - 如何提取 xaml/xml 文件中特定标签的所有出现?

标签 c# xml regex xaml xpath

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

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

最佳答案

这是一个使用 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 的 XPath“老派”风格:

// 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);
}

关于c# - 如何提取 xaml/xml 文件中特定标签的所有出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3942988/

相关文章:

java - 在java中如何欺骗SAX将大块分解成小块?

xml - 使用 XSL 在 tr 类中交替行颜色

java - 添加动画时 Android 应用程序停止

regex - mod重写的奇怪错误

java - 如何在 Java 中模式匹配 [ 和 ]?

python - Django 中的 URL 模式正则表达式

c# - 在 native iOS/Android 应用程序中使用 Xamarin 库

c# - Excel 电子表格的自定义 LINQ 提供程序?

c# - Excel 单元格中的 OpenXml 和日期格式

c# - 我可以在 ASP .Net Core 中使用 TraceSource 进行日志记录吗?