1
我有這樣的代碼來讀取文本,然後分析它爲XML:XML解析在XAML/WinRT中:非法字符在路徑
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"Assets\levels.xml");
var stream = await file.OpenReadAsync();
var rdr = new StreamReader(stream.AsStream());
var contents = await rdr.ReadToEndAsync();
var cleanedContents = contents.Replace("\r\n", "").Replace('\\', ' ');
var xmlElements = XDocument.Load(cleanedContents).Elements();
var levels = xmlElements.Descendants("Level");
這裏是XML:
<?xml version="1.0" encoding="utf-8" ?>
<LevelLoader>
<Level>
<LevelNum>1</LevelNum>
<Waves>
<Wave>
<WaveNum>1</WaveNum>
<Background>
<MinNoise>180</MinNoise>
<MaxNoise>255</MaxNoise>
</Background>
<Objects>
<Object>
<Id>Sat</Id>
<Position>
<Required>1</Required>
<X>2000</X>
<Y>3000</Y>
</Position>
</Object>
</Objects>
</Wave>
</Waves>
</Level>
</LevelLoader>
調試器表明了一堆回車和換行符的得到回升(\ r \ n):
<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n<LevelLoader>\r\n <Level>\r\n <LevelNum>1</LevelNum>\r\n <Waves>\r\n <Wave>\r\n <WaveNum>1</WaveNum>\r\n <Background>\r\n <MinNoise>180</MinNoise>\r\n <MaxNoise>255</MaxNoise>\r\n </Background>\r\n <Objects>\r\n <Object>\r\n <Id>Sat</Id>\r\n <Position>\r\n <Required>1</Required>\r\n <X>2000</X>\r\n <Y>3000</Y>\r\n </Position>\r\n </Object>\r\n </Objects>\r\n </Wave>\r\n </Waves>\r\n </Level>\r\n</LevelLoader>
我似乎能夠剔除說出來好了替換。
我不認爲帶有?的標頭會有所作爲,因爲即使刪除它,我仍然會收到此錯誤。
我不知道如何刪除多餘的\'因爲.replace('\','')似乎並沒有這樣做。
<?xml version=\"1.0\" encoding=\"utf-8\" ?><LevelLoader> <Level> <LevelNum>1</LevelNum> <Waves> <Wave> <WaveNum>1</WaveNum> <Background> <MinNoise>180</MinNoise> <MaxNoise>255</MaxNoise> </Background> <Objects> <Object> <Id>Sat</Id> <Position> <Required>1</Required> <X>2000</X> <Y>3000</Y> </Position> </Object> </Objects> </Wave> </Waves> </Level></LevelLoader>
儘管我仍然看到:
非法字符的路徑。
有什麼建議嗎?
啊。尷尬。謝謝! – micahhoover 2013-03-06 03:53:39
Nah,簡單的混亂......我犯過幾次相同的錯誤 – Luis 2013-03-06 14:03:36