Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to load a string that contains XML that is downloaded from SkyDrive.

XmlDocument myXML = new XmlDocument();
myXML.LoadXml(importXMLDocument);

When I call the above code I get the following error:

Exception from HRESULT: 0xC00CE556

This is the XML that I'm trying to convert from a string and load to the XML Document:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfVehicle xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <vehicle>
    <VehicleName>Tahoe</VehicleName>
    <VehicleYear>2004</VehicleYear>
    <Odometer>97742</Odometer>
    <LicensePlate></LicensePlate>
    <OilWeight>5w-30</OilWeight>
    <OilBrand></OilBrand>
    <OilQuantity>6</OilQuantity>
    <OilFilterModelNumber></OilFilterModelNumber>
    <AirFilterModelNumber></AirFilterModelNumber>
    <TirePressureAll>0</TirePressureAll>
    <TirePressureFrontRight>0</TirePressureFrontRight>
    <TirePressureFrontLeft>0</TirePressureFrontLeft>
    <TirePressureBackRight>0</TirePressureBackRight>
    <TirePressureBackLeft>0</TirePressureBackLeft>
    <OilChangedOdometer>97742</OilChangedOdometer>
    <OilChangedDate>2012-05-04T19:53:53.358-06:00</OilChangedDate>
    <NextOilChangeDate>2012-08-04T19:53:53.358-06:00</NextOilChangeDate>
    <NextOilChangeOdometer>100742</NextOilChangeOdometer>
    <TiresRotated>false</TiresRotated>
    <AirFilterChanged>false</AirFilterChanged>
    <SettingDistance>3000</SettingDistance>
    <SettingMonths>3</SettingMonths>
    <SettingReminder>true</SettingReminder>
    <SettingLiveTile>true</SettingLiveTile>
    <IsTrial>true</IsTrial>
    <VehicleId>2</VehicleId>
  </vehicle>
  <vehicle>
    <VehicleName>Mazda3</VehicleName>
    <VehicleYear>2011</VehicleYear>
    <Odometer>21504</Odometer>
    <LicensePlate>abcdefg</LicensePlate>
    <OilWeight>0w-20</OilWeight>
    <OilBrand></OilBrand>
    <OilQuantity>0</OilQuantity>
    <OilFilterModelNumber></OilFilterModelNumber>
    <AirFilterModelNumber></AirFilterModelNumber>
    <TirePressureAll>0</TirePressureAll>
    <TirePressureFrontRight>0</TirePressureFrontRight>
    <TirePressureFrontLeft>0</TirePressureFrontLeft>
    <TirePressureBackRight>0</TirePressureBackRight>
    <TirePressureBackLeft>0</TirePressureBackLeft>
    <OilChangedOdometer>21504</OilChangedOdometer>
    <OilChangedDate>2012-09-14T18:05:02.298-06:00</OilChangedDate>
    <NextOilChangeDate>2013-02-14T18:05:02.298-07:00</NextOilChangeDate>
    <NextOilChangeOdometer>26504</NextOilChangeOdometer>
    <TiresRotated>false</TiresRotated>
    <AirFilterChanged>false</AirFilterChanged>
    <OilChangeCost>64.75</OilChangeCost>
    <OilChangeNotes>need new tires - $500+</OilChangeNotes>
    <SettingDistance>5000</SettingDistance>
    <SettingMonths>5</SettingMonths>
    <SettingReminder>true</SettingReminder>
    <SettingLiveTile>true</SettingLiveTile>
    <IsTrial>false</IsTrial>
    <VehicleId>2</VehicleId>
  </vehicle>
</ArrayOfVehicle>

Update:

This is the code where I download the XML file from SkyDrive (using the API): It was confirmed last night that this process when the file is being downloaded from SKYDrive that an extra "?" is being add. The following is my entire function that does the downloading and the "LoadXml" call. Any help is appreciated.

private async void readFileInfo(string folderId)
{
     LiveOperationResult operationResultFile =
     await client.GetAsync(folderId + "/files");

     dynamic resultFile = operationResultFile.Result;
     IDictionary<string, object> fileData = (IDictionary<string, object>)resultFile;
     List<object> files = (List<object>)fileData["data"];

     foreach (object item in files)
     {
          IDictionary<string, object> file = (IDictionary<string, object>)item;

          if (file["name"].ToString() == "ocha.txt")
          {
               LiveDownloadOperationResult DLFile =
               await client.BackgroundDownloadAsync(file["source"].ToString();


                var stream = await DLFile.GetRandomAccessStreamAsync();
                var readStream = stream.GetInputStreamAt(0);


                DataReader reader = new DataReader(readStream);
                uint fileLength = await reader.LoadAsync((uint)stream.Size);


                string content = reader.ReadString(fileLength);


                XmlDocument myXML = new XmlDocument();
                myXML.LoadXml(content.ToString());


                VM.importVehicles(content);


                break;
            }
      }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
220 views
Welcome To Ask or Share your Answers For Others

1 Answer

I was able to reproduce the error even with reading a local file. The reason for the error is that the DataReader puts some extra Bytes before the content. You don't see them in the debugger, but when putting the read content in Notepad++ for example, you get an extra question mark:

?<?xml version="1.0" encoding="utf-8"?>

As I suspected the extra bytes were the Byte Order Mark (BOM) bytes (0xEF 0xBB 0xBF (239 187 191)).

I tried to set the encoding for the DataReader explicitly to UTF8 but that did not change anything. Seems to be a bug in the DataReader. B.T.W.. You would get the same error when reading the bytes from the DataReader and try to convert them using Encoding.UTF8.GetString. Even that method does not recognize the BOM.

Okay. Two workarounds:

1) Use FileIO.ReadTextAsync:

string content = await FileIO.ReadTextAsync(file);

2) Use a StreamReader:

using (var stream = await file.OpenReadAsync())
{
   using (var readStream = stream.AsStreamForRead())
   {
      using (StreamReader streamReader = new StreamReader(readStream))
      {
         string content = streamReader.ReadToEnd();
         XmlDocument doc = new XmlDocument();
         doc.LoadXml(content);
      }
   }
}

Update:

The method ReadFileInfo would look like this to avoid the BOM problem. Note that AsStreamForRead is an extension method available in System.IO (put using System.IO; in your code).

private async Task ReadFileInfo(string folderId)
{
   LiveOperationResult operationResultFile =
   await client.GetAsync(folderId + "/files");    

   dynamic resultFile = operationResultFile.Result;
   IDictionary<string, object> fileData = (IDictionary<string, object>)resultFile;
   List<object> files = (List<object>)fileData["data"];

   foreach (object item in files)
   {
      IDictionary<string, object> file = (IDictionary<string, object>)item; 

      if (file["name"].ToString() == "ocha.txt")
      {
         LiveDownloadOperationResult DLFile =
             await client.BackgroundDownloadAsync(file["source"].ToString());  

         using (var stream = await DLFile.GetRandomAccessStreamAsync())
         {
            using (var readStream = stream.AsStreamForRead())
            {
               using (StreamReader streamReader = new StreamReader(readStream))
               {
                  string content = streamReader.ReadToEnd();
                  XmlDocument doc = new XmlDocument();
                  doc.LoadXml(content); 

                  VM.importVehicles(content); 

                  break;
               }
            }
         }
      }
   }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...