Read (and traverse) XAML file from MemoryStream

Jayme65

Active member
Joined
Apr 5, 2011
Messages
35
Programming Experience
Beginner
Hi,

I have an XAML document that I have to read from a MemoryStream (for the purpose of the example, I load a XAML file to a memorystream)
Here is a shortened version of the XAML file:
VB.NET:
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib" >
    <Grid.Resources>
        <System:Decimal x:Key="example">-1</System:Decimal>
    </Grid.Resources>
</Grid>

I have to traverse the document and get the "-1" value from the <SystemDecimal> tag
How should I please do...Here is what I've achieved until now..without success:
VB.NET:
StreamReader mysr = new StreamReader("example.xaml");
XmlDocument doc = new XmlDocument();
doc.Load(mysr);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("System", "clr-namespace:System;assembly=mscorlib");
XmlNode node = doc.SelectSingleNode("/Grid/Grid.Resources/System:Decimal", nsmgr);
Debug.Print(node.InnerText);

Thanks for your help or advice!
 
Last edited:
Elements Grid and Grid.Resources belong to a namespace and your xpath query doesn't specify that.
 
Thanks for your reply! What should I then do please?
Add nsmgr.AddNamespace("ns", "http://schemas.microsoft.com/winfx/2006/xaml/presentation")
nsmgr.AddNamespace("x", "http://schemas.microsoft.com/winfx/2006/xaml")
?
I've done it but 'node' is still 'Null'
 
How does your xpath looks like now then? Are you specifying the 'ns' namespace for those elements?

And you don't need the 'x' namespace, only the Key attribute belongs to that.
 
Thanks for your help!! Here's a working solution:
VB.NET:
StreamReader mysr = new StreamReader("example.xaml");
XmlDocument doc = new XmlDocument();
doc.Load(mysr);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
[B]nsmgr.AddNamespace("def", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
[/B]nsmgr.AddNamespace("System", "clr-namespace:System;assembly=mscorlib");
XmlNode node = doc.SelectSingleNode("/[B]def:[/B]Grid/[B]def:[/B]Grid.Resources/System:Decimal", nsmgr);
Debug.Print(node.InnerText);
 

Latest posts

Back
Top