如何使用XDocument.Load加载带有特殊字符的XML [英] How to load XML with special characters using XDocument.Load

查看:267
本文介绍了如何使用XDocument.Load加载带有特殊字符的XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

**这是针对Silverlight应用程序的.

**This is for silverlight application.

我有一个带有xml数据的流,该数据流在加载消息后会生成数据并生成最终文件.我首先使用XDocument.Load(stream)进行加载.其中一个文件无法加载,经过一番研究,我发现其中一个元素的值为"First& Second",并且在&上加载失败.

I have a stream with xml data, which after loading massage the data and generate final file. I start by load using XDocument.Load(stream). One of the file was failing to load and after some research I foung out that one of the element has a value of 'First & Second' and load fails on hitting &.

是否可以加载带有特殊字符的XML? (我肯定答案是否",但值得一问).

Is it possible to load XML with special characters? ( I am sure the answer is no, but worth asking).

是否可以对XML进行预处理(不影响性能)并将特殊字符更改为其他字符,然后将其恢复为正常值.

Is it possible to preprocess the XML (without performance hit) and change the special characters to someother characters and after the process put it back to normal value.

推荐答案

在XML中,&应该为&,否则它不是有效的XML.如果您无法控制XML文件,请以文本形式加载它们,将&替换为内存中的&(但请注意不要替换作为现有& & >或<!)并再次将它们加载为XML.

In XML, & should be & otherwise it's not valid XML. If you don't have control over the XML files, load them as text, replace & with & in memory (but be careful not to replace the & that are part of entities like existing & > or < !) and load them as XML again.

这篇关于如何使用XDocument.Load加载带有特殊字符的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆