使用c#编辑xml文件,获取错误无效令牌 [英] edit xml file using c#,getting error invalid token

查看:356
本文介绍了使用c#编辑xml文件,获取错误无效令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我的XML文件有两个命名空间,所以我无法改变文本的xml在选择节点方法

My Problem Is My XML file having Two namespace ,so i cant change text of xml in SelectingNode Method

XmlTextReader reader = new XmlTextReader("C:\\test.xml");
                    XmlDocument doc = new XmlDocument();
                    doc.Load(reader); //Assuming reader is your XmlReader
                    XmlNamespaceManager oManager = new XmlNamespaceManager(doc.NameTable);
                    oManager.AddNamespace("ns", "http://schemas.microsoft.com//sqlserver//reporting//2005/01//reportdefinition");
                    oManager.AddNamespace("rd", "http://schemas.microsoft.com//SQLServer//reporting//reportdesigner");           
                    doc.SelectSingleNode("/ns:Report/ns:buttons/ns:workshop1", oManager).InnerText = "new text";
                    reader.Close();
                    doc.Save(@"C:\\test.xml"); 

Xml文件有多个命名空间,所以问题在这里icant解​​决它。

Xml File is Having More than One namespace so problem is here icant resolve it.

和我的XML文件是

   <?xml version="1.0"?>
<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
  <buttons>
    <workshop1>Google</workshop1>
    <url1>www.google.co.uk</url1>
  </buttons>
</Report>


推荐答案

正如@MatthewStrawbridge在评论中提出的,问题是前缀声明指向不正确的命名空间URL。您不需要转义前面的斜杠,因此指向默认命名空间的前缀应该以这种方式声明:

As suggested by @MatthewStrawbridge in comment, the problem is the prefixes declared pointing to incorrect namespace URL. You don't need to escape forward slashes, so the prefix that point to default namespace should be declared this way :

oManager.AddNamespace("ns", "http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition");

然后你可以很好地改变< workshop1> / code>元素到新文本。你甚至不需要在代码中声明第二个前缀映射,因为你不使用它。

Then your could work just fine to change value of <workshop1> element to "new text". You don't even need to declare the second prefix mapping in the code, because you don't use it.

这篇关于使用c#编辑xml文件,获取错误无效令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆