XMLException未得到处理' var'是一个意外的令牌。预期的令牌是' ='

时间:2015-04-05 12:07:50

标签: c# xml

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

  

System.Xml.XmlException:'src'是一个意外的令牌。预期的标记是'='。第29行,第19位。   在System.Xml.XmlTextReaderImpl.Throw(String res,String [] args)    在System.Xml.XmlTextReaderImpl.ParseAttributes()    在System.Xml.XmlTextReaderImpl.ParseElement()    在System.Xml.XmlTextReaderImpl.ParseElementContent()     在System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)     在System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)    在System.Xml.XmlDocument.Load(XmlReader阅读器)     在System.Xml.XmlDocument.Load(String filename)     在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>

感谢您的帮助! :)

1 个答案:

答案 0 :(得分:0)

这对我有用。请注意,我使用的是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);