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

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

问题描述

我的问题是我的 XML 文件有两个命名空间,所以我无法在 SelectingNode 方法中更改 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 文件有多个命名空间,所以问题在这里无法解决.

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> 元素的值更改为 "new text".您甚至不需要在代码中声明第二个前缀映射,因为您不使用它.

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天全站免登陆