micahhoover
micahhoover

Reputation: 2160

xml parsing in xaml/winrt: illegal characters in path

I have this code to read in text and then parse it as XML:

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");

Here is the 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>

The debugger shows that a bunch of carriage returns and newlines get picked up (\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>

I seem to be able to weed that out okay with a replace.

I don't think the header with the ?'s makes a difference because I still get this error even when I remove it.

I'm not sure how to remove the extra \'s because .replace( '\', ' ') doesn't seem to do it.

<?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>

With all that I still see:

Illegal characters in path.

Any suggestions?

Upvotes: 1

Views: 495

Answers (1)

Luis
Luis

Reputation: 1235

XDocument.Load() expects a path; you're passing a string, so you should call Parse()

http://msdn.microsoft.com/en-us/library/bb343181.aspx

Upvotes: 4

Related Questions