【问题标题】:XmlException unhandledXmlException 未处理
【发布时间】:2013-12-17 18:55:11
【问题描述】:

我想加载一个通过从我的数据库中获取数据而创建的 XML 文件。 但是,如果我想读取该数据,它会显示第 1 行有错误。 第 1 行确实很奇怪,因为我从未要求过“ArrayOfBand”。但即使它在那里,当我设置一个断点时,我也会得到我所有的乐队。它只是在最后一步崩溃。错误 HRESULT:0xC00CE556

本地主机上的 XAML

<ArrayOfBand xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://schemas.datacontract.org/2004/07/WindowsStoreApp.Models">
  <Band>
    <Description>Noorse DJ</Description>
    <Facebook>Avicii</Facebook>
    <ID>156</ID>
    <Name>Avicii</Name>
    <Picture>..\Images\Avicii.jpg</Picture>
    <Twitter>#Avicii</Twitter>
  </Band>
  <Band>
    <Description>Heavy Metal</Description>
    <Facebook>A7X</Facebook>
    <ID>157</ID>
    <Name>Avenged Sevenfold</Name>
    <Picture>..\Images\A7X.jpg</Picture>
    <Twitter>#A7X</Twitter>
 </Band>
</ArrayOfBand>

C# xml 阅读器

     string m_strFilePath = "http://localhost:17281/api/Band";
        XmlDocument myXmlDocument = new XmlDocument();
        myXmlDocument.Load(m_strFilePath); //Load NOT LoadXml

        XmlNodeList elemList = myXmlDocument.GetElementsByTagName("Band");

        for (int i = 0; i < elemList.Count; i++)
        {
            XAML station = new XAML() {
                                 ID = elemList[i].Attributes["ID"].InnerText,
                                 Name = elemList[i].Attributes["Name"].InnerText
                               };
            list.Add(station);
        }

        return list;
        }

c# for creating the xml file

您可以看到我不是要“ArrayOfBand”。如果我获取文件并删除“ArrayOfBand”,它会完美运行。任何人都知道它为什么在那里? 公共静态列表 GetBands() { 列表列表 = 新列表(); DbDataReader reader = Database.GetData("SELECT * FROM Band");

            while (reader.Read())
            {
                Band b = new Band();
                b.ID = reader["ID"].ToString();
                b.Name = reader["Name"].ToString();
                b.Picture = reader["Picture"].ToString();
                b.Description = reader["Description"].ToString();
                b.Twitter = reader["Twitter"].ToString();
                b.Facebook = reader["Facebook"].ToString();
                list.Add(b);
            }

            return list;
        }

【问题讨论】:

标签: c# xml


【解决方案1】:

您的 xml 格式错误。最后你需要有一个结束&lt;/ArrayOfBand&gt;

【讨论】:

  • 我有,只是为了节省空间不包括在内
  • @user2827958 - 你并没有真正节省空间我从大约 20 行中删除了一行,这会引起混乱。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-27
  • 1970-01-01
  • 1970-01-01
  • 2015-06-10
  • 1970-01-01
  • 2012-09-23
  • 1970-01-01
相关资源
最近更新 更多