2015-04-05 28 views
0

我正在尝试从xml文件中读取xml节点值。但是当我这样做时会抛出这个异常:XMLException未处理'var'是一个意外的标记。预期的标记是'='

System.Xml.XmlException:'src'是一个意外的标记。预期的标记是'='。线29,在System.Xml.XmlTextReaderImpl.Throw位置19. (字符串RES,字串[] args) 在System.Xml.XmlTextReaderImpl.ParseAttributes() 在System.Xml.XmlTextReaderImpl.ParseElement() 在系统。 Xml.XmlTextReaderImpl.ParseElementContent() 在System.Xml.XmlLoader.LoadNode(布尔skipOverWhitespace) 在System.Xml.XmlLoader.LoadDocSequence(XmlDocument的parentDoc) 在System.Xml.XmlDocument.Load(XmlReader中读取) 的系统。 Xml.XmlDocument.Load(字符串文件名) 在ToolkitM9.RVersion.Window_Loaded(对象发件人,RoutedEventArgs e)如 F:\发展\ 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> 

感谢任何帮助! :)

+0

由于在您的xml示例中没有任何src或=我怀疑您正在加载正确的文件。在尝试解析它之前,你是否从Web服务器或类似的地方加载了这个文件? – 2015-04-05 12:13:17

+0

哦,是的,对不起,我忘了提及!我用: '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_ – squabbi 2015-04-05 12:15:26

+0

确切地说,所以你正在解析的“xml”不是你期望的那样,而是一些错误信息,如html。由于它在通过常规浏览器访问它时似乎有效,因此它可能需要发送一些标题,例如用户代理,接受或以其他方式表示XmlDocument不会为您设置。 – 2015-04-05 12:52:00

回答

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); 
+0

非常感谢!我现在工作! :) – squabbi 2015-04-06 07:40:48

相关问题