我正在将字符串加载到包含以下结构的XML文档:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Include="clsWorker.cs" />
</ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
然后我将所有加载到xmldocument:
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(Xml);
Run Code Online (Sandbox Code Playgroud)
然后出现以下问题:
XmlNode Node = xmldoc.SelectSingleNode("//Compile"); // return null
Run Code Online (Sandbox Code Playgroud)
当我从根元素(Project)中删除xmlns属性时,它的工作正常,如何改进我的SelectSingleNode以返回相关元素?
为什么这个Xpath无法使用XDocument.XPathSelectElement?
Xpath的:
//Plugin/UI[1]/PluginPageCategory[1]/Page[1]/Group[1]/CommandRef[2]
Run Code Online (Sandbox Code Playgroud)
XML
<Plugin xmlns="http://www.MyNamespace.ca/MyPath">
<UI>
<PluginPageCategory>
<Page>
<Group>
<CommandRef>
<Images>
</Images>
</CommandRef>
<CommandRef>
<Images>
</Images>
</CommandRef>
</Group>
</Page>
</PluginPageCategory>
</UI>
</Plugin>
Run Code Online (Sandbox Code Playgroud)
C#代码:
myXDocument.XPathSelectElement("//Plugin/UI[1]/PluginPageCategory[1]/Page[1]/Group[1]/CommandRef[2]", myXDocument.Root.CreateNavigator());
Run Code Online (Sandbox Code Playgroud) 我有一些默认命名空间的xml
<a xmlns='urn:test.Schema'><b/><b/></a>
Run Code Online (Sandbox Code Playgroud)
并想要数数 <b/>
我该如何定义
XmlNamespaceManager nsmgr = ????
Assert.AreEqual(2, doc.SelectNodes("//b", nsmgr).Count);
Run Code Online (Sandbox Code Playgroud)
断言变为真?
我到目前为止尝试过(使用nunit):
[Test]
[Ignore("Why does this not work?")]
public void __DoesNotWork_TestSelectWithDefaultNamespace()
{
// xml to parse with defaultnamespace
string xml = @"<a xmlns='urn:test.Schema'><b/><b/></a>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
// fails because xpath does not have the namespace
//!!!!
Assert.AreEqual(2, doc.SelectNodes("//b").Count);
// using XPath defaultnamespace
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("", "urn:test.Schema");
// This will fail with dotnet 3.5sp1. Why?
//!!!!
Assert.AreEqual(2, doc.SelectNodes("//b", nsmgr).Count);
}
[Test]
public …Run Code Online (Sandbox Code Playgroud) 重新代码:
var tmpNewNode = xdoc.ImportNode(newNode, true);
if (oldNode.ParentNode != null)
{
oldNode.ParentNode.ReplaceChild(tmpNewNode, oldNode);
return true;
}
Run Code Online (Sandbox Code Playgroud)
使用空xmlns属性(xmlns ="")创建tmpNewNode.有什么建议我怎么能避免呢?
10倍
我正在尝试解析网页以从论坛获取帖子.
每条消息的开头都以以下格式开头
<div id="post_message_somenumber">
Run Code Online (Sandbox Code Playgroud)
我只想得到第一个
我xpath='//div[starts-with(@id, '"post_message_')]'在yql 尝试没有成功
我还在学习这个,任何人都有建议
当我想使用XPath遍历我的XmlDocument时,我遇到了文档中有许多丑陋的命名空间的问题,所以我开始使用NamespaceManagerXPath.
XML看起来像这样
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<Worksheet ss:Name="KA0100401">
<Table>
<Row>
<Cell>Data</Cell>
</Row>
<!-- more rows... -->
</Table>
</Worksheet>
<Worksheet ss:Name="KA0100402">
<!-- .... --->
</Worksheet>
</Workbook>
Run Code Online (Sandbox Code Playgroud)
现在,从我在本文档中看到的"urn:schemas-microsoft-com:office:spreadsheet"是默认命名空间,因为它位于根元素上.
所以天真地,我配置了NamespaceManager这样的:
XmlDocument document = new XmlDocument();
document.Load(reader);
XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable);
manager.AddNamespace(String.Empty, "urn:schemas-microsoft-com:office:spreadsheet");
manager.AddNamespace("o", "urn:schemas-microsoft-com:office:office");
manager.AddNamespace("x", "urn:schemas-microsoft-com:office:excel");
manager.AddNamespace("ss", "urn:schemas-microsoft-com:office:spreadsheet");
manager.AddNamespace("html", "http://www.w3.org/TR/REC-html40");
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试访问节点时
foreach (XmlNode row in document.SelectNodes("/Workbook/Worksheet[1]/Table/Row", manager))
Run Code Online (Sandbox Code Playgroud)
我从来没有得到任何结果.我的印象是,通过使用空前缀设置第一个命名空间,在搜索该工作空间中的节点时,我不需要设置它.
但是,因为它是在规定AddNamespace的方法:
如果XPath表达式不包含前缀,则假定名称空间统一资源标识符(URI)是空名称空间.
这是为什么?而且,更重要的是:如何访问默认命名空间中的节点,如果不使用前缀将它们设置为空命名空间?
如果我在搜索节点时甚至无法访问它,那么在管理器上设置默认命名空间有什么用呢?
我在这里看到了几个例子,其中Xpath与XmlDocument一起使用,以从XmlDocument节点获取特定属性....
Console.WriteLine(xmlDocument.SelectSingleNode("//dataTemplateSpecification/templates/template/elements/element/@name").Value.ToString());
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我得到一个"对象引用未设置为对象的实例".例外.每当我遇到特定的代码行时.我有一个小测试应用程序,我已经设置为测试不同的东西,然后我把它们放入我的主项目...
这是代码......
namespace ReadXml
{
class Program
{
static void Main(string[] args)
{
//string fulXmlPath = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/templateExample.xml");
XDocument xDocument = XDocument.Load("C:\\Users\\derekww\\Documents\\XML Documents\\templateExample.xml");
XElement elem = xDocument.Element("dataTemplateSpecification"); ;
XmlDocument xmlDocument = new XmlDocument();
StreamReader file = new StreamReader("C:\\Users\\derekww\\Documents\\XML Documents\\templateExample.xml");
xmlDocument.Load(file);
//XmlDocument theDoc = new XmlDocument();
//using (var xmlReader = xDocument.CreateReader())
//{
// xmlDocument.Load(xmlReader);
//}
//Console.WriteLine(elem.ToString());
XmlNode xNode = xmlDocument.SelectSingleNode("//dataTemplateSpecification/templates/template/elements/element");
Console.WriteLine("WORK PLEASE!!!! {0}", xNode.Value.ToString());
//Console.WriteLine(xmlDocument.SelectSingleNode("//dataTemplateSpecification/templates/template/elements/element/@name").Value.ToString());
//Console.WriteLine("This better Work>>>> {0}", xmlDocument.Attributes["/dataTemplateSpecification/templates/template/elements/element/@name"].Value);
Console.ReadLine();
//Console.WriteLine("This better Work>>>> {0}", xmlDocument.SelectSingleNode("//dataTemplateSpecification/templates/template/elements/element/@name").Value);
//foreach (String AttVal in …Run Code Online (Sandbox Code Playgroud)