【问题标题】:XMLException was unhandled 'var' is an unexpected token. The expected token is '='XMLException 未处理 'var' 是一个意外的标记。预期的令牌是'='
【发布时间】:2015-06-10 00:20:54
【问题描述】:

我正在尝试从 xml 文件中读取 xml 节点值。但是当我这样做时会抛出这个异常:

System.Xml.XmlException: 'src' 是一个意外的标记。预期的标记是“=”。第 29 行,第 19 位。 在 System.Xml.XmlTextReaderImpl.Throw(字符串 res,String[] args) 在 System.Xml.XmlTextReaderImpl.ParseAttributes() 在 System.Xml.XmlTextReaderImpl.ParseElement() 在 System.Xml.XmlTextReaderImpl.ParseElementContent() 在 System.Xml.XmlLoader.LoadNode(布尔 skipOverWhitespace) 在 System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc) 在 System.Xml.XmlDocument.Load(XmlReader 阅读器) 在 System.Xml.XmlDocument.Load(字符串文件名) 在 ToolkitM9.RVersion.Window_Loaded(Object sender, RoutedEventArgs e) 中 f:\Development\ToolkitM9\ToolkitM9\RVersion.xaml.cs:48 行

这是我的代码:

            XmlDocument xDoc = new XmlDocument();
            xDoc.Load("Version.xml");

            XmlNodeList name = xDoc.GetElementsByTagName("Name");
            XmlNodeList ver = xDoc.GetElementsByTagName("Version");
            XmlNodeList notes = xDoc.GetElementsByTagName("Notes");
            XmlNodeList openSite = xDoc.GetElementsByTagName("openSite");
            XmlNodeList link = xDoc.GetElementsByTagName("Link");

            MessageBox.Show(
            "Name: " + name[0].InnerText + "\n" +
            "Version: " + ver[0].InnerText + "\n" +
            "Notes: " + notes[0].InnerText + "\n" +
            "Open Link? " + openSite[0].InnerText + "\n" +
            "Link: " + link[0].InnerText + "\n"

这是我的 XML 文件:

 <Recovery>
    <Name>TWRP</Name>
    <Version>2.5.0.3</Version> 
    <Notes>There are some bugs remaining in this build. See here..</Notes>
    <openSite>true</openSite>
    <Link>http://google.com</Link>
 </Recovery>

感谢您的帮助! :)

【问题讨论】:

  • 由于您的 xml 示例中没有任何 src 或 = 我怀疑您正在加载正确的文件。在尝试解析之前,您是否以某种方式从网络服务器或类似服务器加载此文件?
  • 哦,是的,对不起,我忘了提!我用过:xDoc.Load("https://s.basketbuild.com/dl/devs?dl=squabbi/m9/recoveries/" + ToolkitM9.Properties.Settings.Default["Device"] + "/Version.xml"); 其中ToolkitM9.Properties.Settings.Default["Device"] 是一个变量。例如 GSM
  • 没错,所以您正在解析的“xml”不是您所期望的,而是一些错误消息作为 html。由于它在通过常规浏览器访问它时似乎可以工作,因此它可能希望发送一些标头,例如 User-Agent、Accepts 或其他 XmlDocument 不会为您设置的标头。

标签: c# xml


【解决方案1】:

这对我有用。注意我使用的是System.Xml.Linq.XDocument,而不是XmlDocument。虽然XDocument 是首选,但我认为XmlDocument 的使用不是您的问题。我同意@Karl-Johan Sjogren 的观点,即您可能没有解析您认为的内容,尽管我不同意需要设置标题。跳过文件直接加载URI成功:

XDocument doc = XDocument.Load("https://s.basketbuild.com/dl/devs?dl=squabbi/m9/recoveries/GSM/Version.xml");
var name = doc.Root.Element("Name").Value;
var version = doc.Root.Element("Version").Value;
var notes = doc.Root.Element("Notes").Value;
var site = doc.Root.Element("openSite").Value;
var link = doc.Root.Element("Link").Value;
Console.WriteLine(name);
Console.WriteLine(version);
Console.WriteLine(notes);
Console.WriteLine(site);
Console.WriteLine(link);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-10
    • 2012-05-23
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    • 2018-09-20
    相关资源
    最近更新 更多