C # XDocument parse xml format files with namespaces

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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/">
 3   <id>http://api.douban.com/event/10069638</id>
 4   <title>Debugging the Web </title>
 5   <category scheme="http://www.douban.com/2007#kind" term="http://www.douban.com/2007#event.salon"/>
 6   <author>
 7     <link href="http://api.douban.com/people/1057620" rel="self"/>
 8     <link href="http://www.douban.com/people/aka/" rel="alternate"/>
 9     <link href="http://t.douban.com/icon/u1057620-16.jpg" rel="icon"/>
10 
11     <name>王胖子</name>
12     <uri>http://api.douban.com/people/1057620</uri>
13 </author>
14 </entry>

 

<db:attribute name="invite_only">no</db:attribute>

 Www not want to see so many skip, and then see the familiar <author> </ author>, decisively apply the example above procedures, and consequently did not get a run, in the end the problem lie? C # XML offers a lot of class, XDocument, XReader, XPath, XmlDocument , is not what I am using this class not to force ah?
(1) See below where <entry xmlns = " http://www.w3.org/ 2005 / Atom "  , is the xml namespace xmlns meaning.
(2) See Later, xmlns: DB = " http://www.douban.com/xmlns/"  , binding <db: attribute name = "invite_only "> no </ db: attribute> This sentence can I understand,
db is referred to as a namespace for easy writing in front of the name of the element, so <db: attribute> and <attribute>, <gd: attribute > is not the same.
(3) This may be referred to a document in which the difference variable, but a large number of documents or not, so the namespace as well as a full name, is here http://www.douban.com/xmlns/ . In fact, write the full name of what will do for XML Parser for both handled as a string, but a name to think too much trouble, and secondly to take the opportunity to be advertising, so we generally use the URL.

http://www.w3.org/2005/Atom in the end is Gesha ah, even parsing with namespaces xmlns xml file (Preliminary namespace XML parsing in) a short no?
Hey, aware of this on the right, he is referred to as "", an empty string. This is something called default namespace, no prefix that look at all of the namespace. So that <author> is not bare, ah, people actually < " http://www.w3.org/2005/Atom ": author> so bare program can not parse the course.

 So how to resolve it? Here is a sample program, we want to help. This code can be run on WP7.

 

1  String File = @ " C: \ the Users \ v-menlin \ Documents \ Visual Studio 2010 \ the Projects \ the Test \ the Test \ test.xml " ;
 2              XDocument DOC = XDocument.Load (File);
 3              // use following code to a String the parse 
 . 4              // the XDocument XDocument.Parse DOC = (String);
 . 5  
. 6              // for all the XML file did not add similar db: this element, by the following method 
. 7              an XNamespace D = @ " HTTP: // WWW .w3.org / 2005 / the Atom " ;
 . 8              the foreach (the XElement Element in doc.Descendants (D + " title " ))
. 9              {
 10                  Console.WriteLine (element.Value);
 . 11              }
 12 is              // <author> The following contains a <link>, the following example also demonstrates how to read attributes. 
13 is              the foreach (the XElement Element in doc.Descendants (D + " author " ))
 14              {
 15                  the foreach (the XElement inelement in element.Descendants (D + " Link " ))
 16                  {
 . 17                      Console.WriteLine (inelement.Attribute ( " the href " ) .Value);
18 is                      Console.WriteLine (inelement.Attribute ( " the rel " ) .Value);
 . 19                  }
 20 is              }
 21 is  
22 is              Console.WriteLine ();
 23 is              // for colon prefix added elements, use the following code 
24              an XNamespace DB = @ " HTTP : //www.douban.com/xmlns/ " ;
 25              the foreach (the XElement Element in doc.Descendants (DB + " attribute " ))
 26 is              {
 27                  Console.WriteLine (element.Attribute ( "name " ) .Value);
 28                  Console.WriteLine (element.Value);
 29              }
 30              // is only NameSpace head for a moment.
 31 is  
32              // The following are a few other common head directly for use. 
33 is              an XNamespace = Gd @ " http://schemas.google.com/g/2005 " ;
 34 is              an XNamespace OpenSearch = @ " http://a9.com/-/spec/opensearchrss/1.0/ " ;

 

Guess you like

Origin www.cnblogs.com/baylor2019/p/12616622.html