0

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

手枪.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>

//完全的


枪库.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));
        }
    }
}

}


枪械.cs

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

        get
        {
            return gunName;
        }
    }

//缩短

4

1 回答 1

0

您可能应该发布完整的 XML,但我感觉这是字节顺序标记 (BOM) 问题。Visual Studio 并不总是对 BOM 做正确的事情。将 XML 复制并粘贴到另一个编辑器中并保存。Notepad、Notepad++ 等。下面是一篇文章,其中包含有关该问题的更多信息。

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

埃里克

于 2012-07-11T01:14:24.383 回答