I have a requirement to convert string to XmlNode and add it to existing Xaml file.
我需要将字符串转换为XmlNode并将其添加到现有的Xaml文件中。
My Xml string contains special characters.
我的Xml字符串包含特殊字符。
Here is my Xml string(which comes from the T4 template) which contains "" as attribute value.
这是我的Xml字符串(来自T4模板),其中包含“”作为属性值。
<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 is the code(partial) which converts Xml String to Node and append it to existing file.
下面是将Xml String转换为Node并将其附加到现有文件的代码(部分)。
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 “” got replaced with “î„”.
这里“”被替换为“。
Any idea how can I stop escaping the special characters while reading/writing XML?
知道如何在读/写XML时停止转义特殊字符?
2 个解决方案
#1
6
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.
默认情况下,XML以UTF8编码写出。在UTF8中,ASCII范围之上的字符表示为2到3个字节的序列。我没有检查过,但我认为XML序列化程序完全正确,如果你解析XML,你将得到你的0xE10F字符。从XML的角度来看,这是一个没有区别的差异。
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.
如果您真的想要使用数字字符实体,请告诉序列化程序使用ASCII作为其编码。 ASCII不能直接表示0xE10F字符,序列化器将实现此功能并使用字符实体来解决问题。
#2
2
You can either use base64 encoding or use the CDATA tag for storing complex data in XML.
您可以使用base64编码或使用CDATA标记以XML格式存储复杂数据。
For CDATA example refer to http://www.w3schools.com/xml/xml_cdata.asp
有关CDATA示例,请参阅http://www.w3schools.com/xml/xml_cdata.asp
#1
6
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.
默认情况下,XML以UTF8编码写出。在UTF8中,ASCII范围之上的字符表示为2到3个字节的序列。我没有检查过,但我认为XML序列化程序完全正确,如果你解析XML,你将得到你的0xE10F字符。从XML的角度来看,这是一个没有区别的差异。
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.
如果您真的想要使用数字字符实体,请告诉序列化程序使用ASCII作为其编码。 ASCII不能直接表示0xE10F字符,序列化器将实现此功能并使用字符实体来解决问题。
#2
2
You can either use base64 encoding or use the CDATA tag for storing complex data in XML.
您可以使用base64编码或使用CDATA标记以XML格式存储复杂数据。
For CDATA example refer to http://www.w3schools.com/xml/xml_cdata.asp
有关CDATA示例,请参阅http://www.w3schools.com/xml/xml_cdata.asp