C# 我在这里检查不使用';不存在?

C# 我在这里检查不使用';不存在?,c#,xml,C#,Xml,我的XML: 我本以为我会得到这样的结果:“这里没有咖啡,但我也会得到”小家伙,没有任何新书“。在检查不存在的xml节点时,我做错了什么 编辑: 与另一个方面斗争。当存在子命名空间时会发生什么 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace MoreXMLT

我的XML:

我本以为我会得到这样的结果:“这里没有咖啡,但我也会得到”小家伙,没有任何新书“。在检查不存在的xml节点时,我做错了什么

编辑:

与另一个方面斗争。当存在子命名空间时会发生什么

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace MoreXMLTesting
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create the XmlDocument.
            XmlDocument doc = new XmlDocument();
            doc.Load("newbooks.xml");

            if (doc.SelectSingleNode("/bookstore/book/title") == null)
            {
                Console.WriteLine("Whelp, there aren't any new books");
            }
            if (doc.SelectSingleNode("/bookstore/coffee") == null);
            {
                Console.WriteLine("There ain't coffee at this joint");
            }

            Console.ReadKey();
        }
    }
}

女仆的故事
玛格丽特
阿特伍德
19.95

您需要尊重并处理XML文档中的XML名称空间

<?xml version='1.0'?>
<bookstore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\newbooks.xsd">
  <book genre="novel" style="hardcover">
    <title>The Handmaid's Tale</title>
    <author>
      <first-name>Margaret</first-name>
      <last-name>Atwood</last-name>
    </author>
    <price>19.95</price>
  </book>
</bookstore>

您需要尊重并处理XML文档中的XML名称空间

<?xml version='1.0'?>
<bookstore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\newbooks.xsd">
  <book genre="novel" style="hardcover">
    <title>The Handmaid's Tale</title>
    <author>
      <first-name>Margaret</first-name>
      <last-name>Atwood</last-name>
    </author>
    <price>19.95</price>
  </book>
</bookstore>

尝试删除XML中的单引号您需要为newbooks架构添加名称空间管理器,并在查询节点时使用该名称空间管理器。添加名称空间:尝试删除XML中的单引号您需要为newbooks架构添加名称空间管理器,并在查询节点时使用该名称空间管理器。添加名称空间:感谢bunch!正如你所说,我只是在学习XML内容。接下来的问题。每个XML都有一个名称空间吗?@BradB:每个XML都有一个隐式名称空间-如果没有显式表示的话-但这感觉有点像“没有名称空间”,谢谢,听起来不错。我已经有另一个问题了。我有这个问题:““xmlns:xsi”是如何实现的“在Xpath和AddNamespace中获得地址?只需添加
nsmgr.AddNamespace(“xsi”,“w3.org/2001/XMLSchema实例”)
并在需要的地方使用
xsi
前缀。我不知道我为什么要这么费劲地使用这个前缀。非常感谢你的帮助,希望你能多帮点忙。我已经对原始问题进行了编辑,以包含放在我膝上的奇怪名称空间项。我已经读了很多书,但不知道如何用奇怪的名称空间命名法检查节点。我发现的唯一一件事是,有人说,如果他们不打算使用xml文件,就不应该给它命名。小子,这是我一直在处理的文件。再次感谢你。真的,非常感谢!正如你所说,我只是在学习XML。跟进问题。每个XML都有一个名称空间吗?@BradB.:每个XML都有一个隐式名称空间——如果没有显式表示的话——但这感觉有点像“没有名称空间”,谢谢,听起来不错。我已经有另一个问题了。我有这个:。“xmlns:xsi”如何在Xpath和AddNamespace中寻址?只需添加
nsmgr.AddNamespace(“xsi”,“w3.org/2001/XMLSchema实例”)
并在需要的地方使用
xsi
前缀。我不知道我为什么要这么费劲地使用这个前缀。非常感谢你的帮助,希望你能多帮点忙。我已经对原始问题进行了编辑,以包含放在我膝上的奇怪名称空间项。我已经读了很多书,但不知道如何用奇怪的名称空间命名法检查节点。我发现的唯一一件事是,有人说,如果他们不打算使用xml文件,就不应该给它命名。小子,这是我一直在处理的文件。再次感谢你。真正地
<bookstore xmlns="urn:newbooks-schema">
           ***************************
// Create the XmlDocument.
XmlDocument doc = new XmlDocument();

// Create the XML Namespace Manager and initialize it    
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ns", "urn:newbooks-schema");

doc.Load("newbooks.xml");

// *USE* the namespace prefix as defined by your "nsmgr" in your XPath
if (doc.SelectSingleNode("/ns:bookstore/ns:book/ns:title", nsmgr) == null)
{
    Console.WriteLine("Whelp, there aren't any new books");
}

if (doc.SelectSingleNode("/ns:bookstore/ns:coffee", nsmgr) == null) ;
{
    Console.WriteLine("There ain't coffee at this joint");
}