查询 XmlDocument 时未出现“未定义命名空间前缀"问题 [英] Query an XmlDocument without getting a 'Namespace prefix is not defined' problem

查看:41
本文介绍了查询 XmlDocument 时未出现“未定义命名空间前缀"问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Xml 文档,它定义并引用了一些命名空间.我将它加载到 XmlDocument 对象中,据我所知,我创建了一个 XmlNamespaceManager 对象,用于查询 Xpath.问题是我收到了命名空间my"未定义的 XPath 异常.如何让命名空间管理器查看我引用的命名空间是否已经定义.或者更确切地说,我如何从文档中获取命名空间定义到命名空间管理器.

I've got an Xml document that both defines and references some namespaces. I load it into an XmlDocument object and to the best of my knowledge I create a XmlNamespaceManager object with which to query Xpath against. Problem is I'm getting XPath exceptions that the namespace "my" is not defined. How do I get the namespace manager to see that the namespaces I am referencing are already defined. Or rather how do I get the namespace definitions from the document to the namespace manager.

此外 tt 让我感到奇怪,您必须首先为从文档名称表创建的文档提供名称空间管理器.即使您需要对手动命名空间进行硬编码,为什么不能将它们直接添加到文档中.为什么每次查询都必须传递这个命名空间管理器?XmlDocument 不知道什么?

Furthermore tt strikes me as strange that you have to provide a namespace manager to the document which you create from the documents nametable in the first place. Even if you need to hardcode manual namespaces why can't you add them directly to the document. Why do you always have to pass this namespace manager with every single query? What can't XmlDocument just know?

代码:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(programFiles + @"Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\HfscBookingWorkflow\template.xml");
XmlNamespaceManager ns = new XmlNamespaceManager(xmlDoc.NameTable);

XmlNode referenceNode = xmlDoc.SelectSingleNode("/my:myFields/my:ReferenceNumber", ns);
referenceNode.InnerXml = this.bookingData.ReferenceNumber;

XmlNode titleNode = xmlDoc.SelectSingleNode("/my:myFields/my:Title", ns);
titleNode.InnerXml = this.bookingData.FamilyName;

XML:

  <?xml version="1.0" encoding="UTF-8" ?> 
  <?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:Inspection:-myXSD-2010-01-15T18-21-55" solutionVersion="1.0.0.104" productVersion="12.0.0" PIVersion="1.0.0.0" ?> 
  <?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.2"?> 
  <my:myFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2010-01-15T18:21:55" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003">
  <my:DateRequested xsi:nil="true" /> 
  <my:DateVisited xsi:nil="true" /> 
  <my:ReferenceNumber /> 
  <my:FireCall>false</my:FireCall> 

更新:

ns.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
ns.AddNamespace("xhtml", "http://www.w3.org/1999/xhtml");
ns.AddNamespace("xd", "http://schemas.microsoft.com/office/infopath/2003");
ns.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2010-01-15T18:21:55");

这可以完成工作,但这意味着我必须对这个特定的 xml 模式进行硬编码.此架构表示一个 infopath 表单模板.特别是每个表单模板的 my namespace url 都会不同,所以我真的不想对此进行硬编码.最好能找到一种干净的方法从 xml 中获取这个命名空间,而无需求助于 RegEx.

This does the job, but it mean's I have to hard code to this particular xml schema. This schema represents an infopath form template. In particular the my namespace url will be different for every form template so I really don't want to hardcode this. It would be nice to find a clean way to get this namespace from the xml without resorting to RegEx.

我希望 XmlNamespaceManager 能够从 NameTable 中获取命名空间定义.我的意思是它们在 Xml 中,但我仍然需要定义它们.

I was hoping that the XmlNamespaceManager would just sort of pick up the namespace definitions form the NameTable. I mean their in the Xml but I still have to define them.

推荐答案

ns.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
ns.AddNamespace("xhtml", "http://www.w3.org/1999/xhtml");
ns.AddNamespace("xd", "http://schemas.microsoft.com/office/infopath/2003");
ns.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2010-01-15T18:21:55");

这可以完成工作,但这意味着我必须对这个特定的 xml 模式进行硬编码.此架构表示一个 infopath 表单模板.特别是每个表单模板的 my namespace url 都会不同,所以我真的不想对此进行硬编码.最好能找到一种干净的方法从 xml 中获取这个命名空间,而无需求助于 Regex.

This does the job, but it mean's I have to hard code to this particular xml schema. This schema represents an infopath form template. In particular the my namespace url will be different for every form template so I really don't want to hardcode this. It would be nice to find a clean way to get this namespace from the xml without resorting to Regex.

我希望 XmlNamespaceManager 能够从 NameTable 中获取命名空间定义.我的意思是它们在 Xml 中,但我仍然需要定义它们.

I was hoping that the XmlNamespaceManager would just sort of pick up the namespace definitions form the NameTable. I mean their in the Xml but I still have to define them.

这篇关于查询 XmlDocument 时未出现“未定义命名空间前缀"问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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