C#麻烦> “XML文档中存在错误(2,2)”我不确定问题是什么

时间:2012-07-11 00:41:41

标签: c# xml windows-phone-7

我有一个错误(XML文档中存在错误(2,2)。)我一直在为此而烦恼。我似乎无法查明错误。如果一双新鲜的眼睛可以帮助我,我将非常感激。

    public GunPresenter()
    {
        Uri uri = new Uri("http://www.foo.com/handguns.xml");
        WebClient webClient = new WebClient();
        webClient.DownloadStringCompleted += OnDownloadStringCompleted;
        webClient.DownloadStringAsync(uri);
    }

    void OnDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs args)
    {
        StringReader reader = new StringReader(args.Result);
        XmlSerializer xml = new XmlSerializer(typeof(GunLibrary)); **//Error is here**
        GunLibrary = (GunLibrary)xml.Deserialize(reader);

handguns.xml

    <?xml version="1.0" encoding="utf-8"?>
    <HandgunLibrary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <TypeOfGun>Handgun</TypeOfGun>
      <Guns>
        <Gun>
          <GunName>Ballester-Molina</GunName>
          <Type>Single Action</Type>
          <Caliber>.45ACP</Caliber>
          <Capacity>7 rounds</Capacity>
          <WeightUnloaded>1075 grams</WeightUnloaded>
          <Legth>216 mm</Legth>
          <CountryOrigin>Argentine</CountryOrigin>
          <PhotoFileName>http://foo.com/gunImages/ballester.png</PhotoFileName>
          <YearMade>1938-1953</YearMade>
        </Gun>
        <Gun>
          <GunName>Bersa Thunder 9</GunName>
          <Type>Double Action</Type>
          <Caliber>9x19mm Luger/.40S&amp;W</Caliber>
          <Capacity>17(9mm)/13(.40)</Capacity>
          <WeightUnloaded>870 grams</WeightUnloaded>
          <Legth>192 mm</Legth>
          <CountryOrigin>Argentine</CountryOrigin>
          <PhotoFileName>http://foo.com/gunImages/bersathunder9.png</PhotoFileName>
          <YearMade>1994-present</YearMade>
        </Gun>
        <Gun>
          <GunName>Bersa Thunder-mini</GunName>
          <Type>Double Action</Type>
          <Caliber>9x19mm Luger/.40S&amp;W</Caliber>
          <Capacity>13(9mm)/10(.40)</Capacity>
          <WeightUnloaded>765 grams</WeightUnloaded>
          <Legth>165 mm</Legth>
          <CountryOrigin>Argentine</CountryOrigin>
          <PhotoFileName>http://foo.com/gunImages/bersathundermini.png</PhotoFileName>
          <YearMade>1994-present</YearMade>
        </Gun>
        <Gun>
          <GunName>Bersa Thunder Ultra Compact</GunName>
          <Type>Double Action</Type>
          <Caliber>.45ACP</Caliber>
          <Capacity>7</Capacity>
          <WeightUnloaded>780 grams</WeightUnloaded>
          <Legth>173 mm</Legth>
          <CountryOrigin>Argentine</CountryOrigin>
          <PhotoFileName>http://foo.com/gunImages/bersathunderultracompact.png</PhotoFileName>
          <YearMade>1994-present</YearMade>
        </Gun>
        <Gun>
          <GunName>Bersa Thunder-380</GunName>
          <Type>Double Action</Type>
          <Caliber>.380ACP/.32ACP</Caliber>
          <Capacity>7(9mm)/9(7.65mm)</Capacity>
          <WeightUnloaded>560 grams</WeightUnloaded>
          <Legth>168 mm</Legth>
          <CountryOrigin>Argentine</CountryOrigin>
          <PhotoFileName>http://foo.com/gunImages/bersathunder-380.png</PhotoFileName>
          <YearMade>1995-present</YearMade>
        </Gun>
      </Guns>
    </HandgunLibrary>

//完成


GunLibrary.cs

      using System;
      using System.Collections.ObjectModel;
      using System.ComponentModel;
      using System.Xml.Serialization;

      namespace HandgunLibrary
      {

public class GunLibrary : INotifyPropertyChanged
{
    [XmlAnyElement]
    public event PropertyChangedEventHandler PropertyChanged;
    string typeOfGun;
    ObservableCollection<Guns> guns = new ObservableCollection<Guns>();


    public string TypeOfGun
    {
        set
        {
            if (typeOfGun != value)
            {
                typeOfGun = value;
                OnPropertyChanged("TypeOfGun");
            }
        }

        get
        {
            return typeOfGun;
        }
    }

    public ObservableCollection<Guns> Guns
    {
        set
        {
            if (guns != value)
            {
                guns = value;
                OnPropertyChanged("Guns");
            }
        }

        get
        {
            return guns;
        }
    }

    protected virtual void OnPropertyChanged(string propChanged)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propChanged));
        }
    }
}

}


Guns.cs

    public string GunName
    {
        set
        {
            if (gunName != value)
            {
                gunName = value;
                OnPropertyChanged("GunName");
            }
        }

        get
        {
            return gunName;
        }
    }

//缩短

1 个答案:

答案 0 :(得分:0)

您应该发布完整的XML,但我觉得这是一个字节订单标记(BOM)问题。 Visual Studio并不总是使用BOM做正确的事情。将XML复制并粘贴到另一个编辑器中并保存。记事本,Notepad ++等。下面是一篇文章,其中提供了有关该问题的更多信息。

http://blog.fourthwoods.com/2011/07/11/removing-the-byte-order-mark-inserted-in-source-files-by-visual-studio/

埃里克