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 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:
hello world
one
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 happily uses the same program to parse the actual XML file. Below is an XML file returned by a Douban API
Http://api.douban.com/event/10069638
Debugging the Web
Name> fat big head fish
Http://api.douban.com/people/1057620 </uri>
No
If you see so many www sites, you don't want to skip them directly. then you can see the familiarAnd resolutely apply the program in the above example, but nothing can be done 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.
This line does not look at the header, look at the following here NoThis sentence can be understood. db is short for namespace, which is convenient for writing in front of the element name. And, It's 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, so even if both documents have As long as the full name is different, there is no problem.
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 it's not bare. people are 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. This code 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); // for all elements in the XML file that do not have a db like this: the following method XNamespace d = @ "http://www.w3.org/2005/Atom"; foreach (XElement element in doc. descendants (d + "title") {Console. writeLine (element. value);} // contains
The following example also 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 +" Ttribute ") {Console. writeLine (element. attribute ("name "). value); Console. writeLine (element. value);} // 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 ";
The above are the details of namespace in XML parsing. For more information, see other related articles in the first PHP community!