Pratik Mehta
Pratik Mehta

Reputation: 1352

Reading/Writing XML nodes containing special characters

I have a requirement to convert string to XmlNode and add it to existing Xaml file.

My Xml string contains special characters.

Here is my Xml string(which comes from the T4 template) which contains "&#xE10F" as attribute value.

<Button Margin="10,0,0,0" Width="100" Height="100" HorizontalContentAlignment="Center"
            Background="{StaticResource TopAppbarTileBackground}" 
            x:Name="chanceAppBarButton">
        <Grid Margin="5">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
                        <TextBlock ***Text="&#xE10F;"*** VerticalAlignment="Top" HorizontalAlignment="Center" 
                                   TextAlignment="Center" Style="{StaticResource TopAppbarIconStyle}"/>

            <TextBlock  Grid.Row="1" Style="{StaticResource TopAppbarTileTextStyle}" x:Uid="chanceAppBarButtonLabel"
                       VerticalAlignment="Top" Margin="0" HorizontalAlignment="Center" 
                                   TextAlignment="Center"/>
        </Grid></Button>

Here is the code(partial) which converts Xml String to Node and append it to existing file.

 var xmldoc = new XmlDocument();
        xmldoc.Load(filePath);
        XmlElement rootElement = xmldoc.DocumentElement;
     XmlNode xmlNode = CreateNodeFromXmlString(xmlNodeString, namespaceList);  
XmlNode importNode = rootElement.OwnerDocument.ImportNode(xmlNode, true);
     rootElement.AppendChild(importNode);  // Add Xml node to Parent Element
     xmldoc.Save(filePath);  // Save Xml file

     private static XmlNode CreateNodeFromXmlString(string xml, Dictionary<string, string> namespaceList)
    {
        var newDataTemplateDocument = new XmlDocument();
        var nameTable = new NameTable();

        var xmlNamespaceManager = new XmlNamespaceManager(nameTable);

        foreach (var namespaceItem in namespaceList)
        {
            xmlNamespaceManager.AddNamespace(namespaceItem.Key, namespaceItem.Value);
        }

        var context = new XmlParserContext(nameTable, xmlNamespaceManager, null, XmlSpace.None);

        var reader = new XmlTextReader(xml.Trim(), XmlNodeType.Element, context);

        return newDataTemplateDocument.ReadNode(reader);
    }

The output of the above code is(just showing button control):

<Button Margin="10,0,0,0" Width="100" Height="100" HorizontalContentAlignment="Center"
            Background="{StaticResource TopAppbarTileBackground}" 
            x:Name="chanceAppBarButton">
        <Grid Margin="5">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <TextBlock Text=" î„" VerticalAlignment="Top" HorizontalAlignment="Center" 
                                   TextAlignment="Center" Style="{StaticResource TopAppbarIconStyle}"/>

            <TextBlock  Grid.Row="1" Style="{StaticResource TopAppbarTileTextStyle}" x:Uid="chanceAppBarButtonLabel"
                       VerticalAlignment="Top" Margin="0" HorizontalAlignment="Center" 
                                   TextAlignment="Center"/>
        </Grid></Button>

Here “&#xE10F” got replaced with “î„”.

Any idea how can I stop escaping the special characters while reading/writing XML?

Upvotes: 3

Views: 2355

Answers (2)

remudada
remudada

Reputation: 3791

You can either use base64 encoding or use the CDATA tag for storing complex data in XML.

For CDATA example refer to http://www.w3schools.com/xml/xml_cdata.asp

Upvotes: 2

keshlam
keshlam

Reputation: 8058

By default, XML is written out in the UTF8 encoding. In UTF8, characters above the ASCII range are represented as two- to three-byte sequences. I haven't checked, but I would assume that the XML serializer did exactly the right thing and that if you parse the XML you'll get back your 0xE10F character. From XML's point of view, this is a difference that makes no difference.

If you really want the numeric character entity used instead, tell your serializer to use ASCII as its encoding. ASCII can't represent the 0xE10F character directly, and the serializer will realize this and use the character entity to work around the problem.

Upvotes: 6

Related Questions