1

我正在尝试加载一个包含从 SkyDrive 下载的 XML 的字符串。

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

当我调用上面的代码时,我收到以下错误:

HRESULT 异常:0xC00CE556

这是我试图从字符串转换并加载到 XML 文档的 XML:

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

更新:

这是我从 SkyDrive 下载 XML 文件的代码(使用 API):昨晚证实,当从 SKYDrive 下载文件时,这个过程是一个额外的“?” 正在添加。以下是我执行下载和“LoadXml”调用的整个函数。任何帮助表示赞赏。

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;
            }
      }
}
4

1 回答 1

3

即使读取本地文件,我也能够重现该错误。错误的原因是 DataReader 在内容之前放置了一些额外的字节。您在调试器中看不到它们,但是例如在将读取的内容放入 Notepad++ 时,您会得到一个额外的问号:

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

正如我怀疑额外的字节是字节顺序标记(BOM)字节(0xEF 0xBB 0xBF(239 187 191))。

我尝试将 DataReader 的编码显式设置为 UTF8,但这并没有改变任何东西。似乎是 DataReader 中的错误。顺便提一句。从 DataReader 读取字节并尝试使用 Encoding.UTF8.GetString 转换它们时,您会遇到相同的错误。即使该方法也无法识别 BOM。

好的。两种解决方法:

1) 使用 FileIO.ReadTextAsync:

string content = await FileIO.ReadTextAsync(file);

2) 使用 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);
      }
   }
}

更新:

ReadFileInfo 方法看起来像这样,以避免 BOM 问题。请注意, AsStreamForRead 是 System.IO 中可用的扩展方法(使用 System.IO; 在您的代码中)。

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;
               }
            }
         }
      }
   }
}
于 2012-11-02T08:57:20.787 回答