加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

XML

发布时间:2020-12-16 05:44:42 所属栏目:百科 来源:网络整理
导读:XML Namespace If namespace isn’t correct,the SelectSingleNode will return null! If prefix has specified in xmlns,just like: xmlns:prefix_foo=”name-of-namespace”,all element under the namespace must specify this prefix explicitly. For ex

XML

Namespace

  • If namespace isn’t correct,the SelectSingleNode will return null!
  • If prefix has specified in xmlns,just like: xmlns:prefix_foo=”name-of-namespace”,all element under the namespace must specify this prefix explicitly. For example
<prefix_foo:book />
  • Default Namespaces ( xmlns without prefix,for example: xmlns=”name-of-namespace” ): Defining a default namespace for an element saves us from using prefixes in all the child elements.
  • Namespace also can be empty.

Specify a note that contains a value

// Get the note that which value is 'Atwood',using [] to filter the notes.
     note = root.SelectSingleNode("/root/subnote[subsubnote='Atwood']/subsubnote");

Read Value Note

XmlNode.InnerText

Test XML file

<?xml version='1.0'?>
<bookstore xmlns="urn:newbooks-schema">
  <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>
  <book genre="novel" style="other">
    <title>The Poisonwood Bible</title>
    <author>
      <first-name>Barbara</first-name>
      <last-name>Kingsolver</last-name>
    </author>
    <price>11.99</price>
  </book>
</bookstore>

Test Code

using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {

      XmlDocument doc = new XmlDocument();
      doc.Load("newbooks.xml");

      // Create an XmlNamespaceManager to resolve the default namespace.
      XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
      nsmgr.AddNamespace("bk","urn:newbooks-schema");

      // Select the first book written by an author whose last name is Atwood.
      XmlNode book;
      XmlElement root = doc.DocumentElement;
      book = root.SelectSingleNode("descendant::bk:book[bk:author/bk:last-name='Atwood']",nsmgr);

      Console.WriteLine(book.OuterXml);
  }
}

Reference Document

  • https://msdn.microsoft.com/en-us/library/ms256471(v=vs.110).aspx
  • https://en.wikipedia.org/wiki/XPath
  • https://en.wikipedia.org/wiki/XML

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读