A Preliminary Study of namespace in XML Parsing

Source: Internet
Author: User
Tags visual studio 2010

The most common problem for beginners when parsing XML files is the XML namespace. This article aims to give a brief introduction to namespace.

The significance of namespace is needless to say. Like C ++, C #, and other advanced languages, XML also faces the problem of variable name duplication when a large number of files are put together, therefore, use namespace to isolate variables with the same name and different meanings. This article focuses on the namespace parsing method.

The following is a simple XML file:

<Root>

<Child id = '0'>

Hello World

</Child>

<Child id = '1'>

One

</Child>

</Root>

There is no namespace in this example. I am afraid this is the case when you are new to XML. This example is misleading. After a beginner parses Hello world, he will happily take the sameProgramParsing the actual XML file often leads to a crash. Below is an XML file returned by a Douban API

<? XML version = "1.0" encoding = "UTF-8"?>
<Entry xmlns = "http://www.w3.org/2005/Atom" xmlns: DB = "http://www.douban.com/xmlns/" xmlns: Gd = "http://schemas.google.com/g/2005" xmlns: opensearch = "http://a9.com/-/spec/opensearchrss/1.0/">
Http://api.douban.com/event/10069638
<Title> debugging the Web </title>
<Category Scheme = "http://www.douban.com/2007#kind" term = "http://www.douban.com/2007#event.salon"/>
<Author>
<Link href = "http://api.douban.com/people/1057620" rel = "self"/>
<Link href = "http://www.douban.com/people/aka/" rel = "alternate"/>
<Link href = "http://t.douban.com/icon/u1057620-16.jpg" rel = "icon"/>

Name> fat big head fish </Name>
<URI> http://api.douban.com/people/1057620 </uri>
</Author>

<DB: attribute name = "invite_only"> NO </DB: attribute>

If you see so many WWW sites, you don't want to skip them directly. Then, you can see the familiar <author> </author>. You can apply the program in the above example in a decisive way, but you can't get anything at all, what is the problem? C # provide a lot of XML classes, such as xdocument, xreader, XPath, and xmldocument. Isn't this type I am using? I can't decide whether to try it. I just tried it all night. Let's take a look at shoes one by one.

 

<? XML version = "1.0" encoding = "UTF-8"?> This line did not look at the header, see the following here <entry xmlns = "http://www.w3.org/2005/Atom", xmlns is the meaning of XML namespace, this potholes http://www.w3.org/2005/atomto the bottom is what. Look back, xmlns: DB = "http://www.douban.com/xmlns/", combined with <DB: attribute name = "invite_only"> NO </DB: attribute> this sentence, you can understand, DB is short for a namespace. It is easy to write it before the element name. In this way, <DB: attribute> and <GD: attribute> are different. This abbreviation can differentiate variables in a document, but it still does not work for a large number of documents. Therefore, namespace also has a full name: http://www.douban.com/xmlns /. This full name can be used to write anything. For XML parser, it is processed as a string, but it is troublesome to get a name, and you can make an advertisement by the way, so the URLs are generally used. When parse is used, Parser distinguishes variables based on the full name. Therefore, even if both documents have <DB: attribute>, It is okay if the full name is different.

 

This is clear, but the http://www.w3.org/2005/atomis all simple. Ah, that's right. His short name is "", an empty string. This is called the default namespace. All those without a prefix appear under this namespace. So the <author> is not bare, but it is actually <"http://www.w3.org/2005/atom": Author>, so the naked program cannot be parsed.

 

So how should we resolve it? Here is a sample program, hoping to help you. ThisCodeIt can be run on WP7. I also have a version of xmldocument. This class is available on Nima WP7, and it is very boring...

String file = @ "C: \ Users \ v-menlin \ Documents \ Visual Studio 2010 \ projects \ test. xml ";
Xdocument Doc = xdocument. Load (File );
// Use following code to parse a string
// Xdocument Doc = xdocument. parse (string );

// If dB is not added to all elements in the XML file, use the following method:
Xnamespace d = @ "http://www.w3.org/2005/Atom ";
Foreach (xelement element in Doc. descendants (D + "title "))
{
Console. writeline (element. value );
}
// <Author> the following example shows how to read attributes.
Foreach (xelement element in Doc. descendants (D + "author "))
{
Foreach (xelement inelement in element. descendants (D + "Link "))
{
Console. writeline (inelement. Attribute ("href"). value );
Console. writeline (inelement. Attribute ("rel"). value );
}
}

console. writeline ();
// For elements with a colon prefix, use the following code
xnamespace DB = @ "http://www.douban.com/xmlns ";
foreach (xelement element in Doc. descendants (DB + "attribute")
{< br> console. writeline (element. attribute ("name "). value);
console. writeline (element. value);
}< br> // The namespace header is changed.

// Several other common headers are listed below for direct replacement.
xnamespace GD = @ "http://schemas.google.com/g/2005";
xnamespace opensearch = @ "http://a9.com/-/spec/opensearchrss/1.0/";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.