views:

55

answers:

2

Let's say I need to extract all solid-brushes and linear-gradient brushes in a dictionary resource file using C#. How can I do it? Please help!

This question can be extended to be more general like "How to find the multiple-lines match(es) when searching a file using C#?"

A: 

Load the file into an XDocument and use that to find your matches. Regex and xml are not a good match (pun intended).

klausbyskov
+1  A: 

Here's a simple example using Linq to XML that should get you started:

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

There are two ways you can use XPath but to begin with we need to set up an XmlNamespaceManager which is a similar mechanism to the XNamespace class. Both the following examples will use this:

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

Using XPath to query an XDocument and then change the Width attribute on the Save button:

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

Using XPath 'old school' style with an 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);
}
Kev
@Kev: Great! It works for me now. Though, I still miss XPath. Do you have any idea to use XPath?
Nam Gi VU