C#与>&引用;XML文档(2,2)中有一个错误;我不确定是什么问题

C#与>&引用;XML文档(2,2)中有一个错误;我不确定是什么问题,c#,xml,windows-phone-7,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.DownloadStringComple

我有一个错误(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>

//缩短的

您可能应该发布完整的XML,但我觉得这是一个字节顺序标记(Byte Order Mark,BOM)问题。VisualStudio对BOM并不总是做正确的事情。将XML复制并粘贴到另一个编辑器中并保存它。记事本,记事本++等。下面是一篇关于这个问题的文章


Erick

您“缩短”了XML?它看起来根本不像有效的XML。最好逐字记录。您发布了不完整、无效的XML,并希望我们帮助您解决XML问题?我能看到的唯一问题是它不完整和无效。请编辑您的帖子并发布一个完整的(不必太大)XML片段,否则很难帮助您了解它的错误。(在编辑时,您可能也会花时间格式化XML和代码,以便正确缩进并更易于阅读。)感谢您的链接,但在完成您所说的操作后,我仍然有相同的错误。谢谢
      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));
        }
    }
}
    public string GunName
    {
        set
        {
            if (gunName != value)
            {
                gunName = value;
                OnPropertyChanged("GunName");
            }
        }

        get
        {
            return gunName;
        }
    }