2009-07-08 29 views
11

Câu hỏi đơn giản, tôi chỉ muốn chọn văn bản từ thẻ < Mẫu >. Đây là những gì tôi có, nhưng Xpath không phù hợp với bất cứ thứ gì.Cách chọn các nút bằng XPath trong C#?

public static void TestXPath() 
{ 
    string xmlText = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"; 
    xmlText += "<Properties xmlns=\"http://schemas.openxmlformats.org/officeDocument/2006/extended-properties\" xmlns:vt=\"http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes\">"; 
    xmlText += "<Template>Normal</Template> <TotalTime>1</TotalTime> <Pages>1</Pages> <Words>6</Words>"; 
    xmlText += "</Properties>"; 

    XmlDocument xmlDoc = new XmlDocument(); 
    xmlDoc.Load(new System.IO.StringReader(xmlText)); 

    foreach (XmlNode node in xmlDoc.SelectNodes("//Template")) 
    { 
     Console.WriteLine("{0}: {1}", node.Name, node.InnerText); 
    } 
} 

Trả lời

24

Bạn cần phải sử dụng một XmlNamespaceManager vì các yếu tố Template là trong một không gian tên:

XmlDocument xmlDoc = new XmlDocument(); 
xmlDoc.Load(new System.IO.StringReader(xmlText)); 
XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable); 
manager.AddNamespace("ns", 
    "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"); 

foreach (XmlNode node in xmlDoc.SelectNodes("//ns:Template", manager)) 
{ 
    Console.WriteLine("{0}: {1}", node.Name, node.InnerText); 
} 
7

Đó là một vấn đề không gian tên; bạn cần lấy bảng tên, chọn bí danh và sử dụng tên đó trong truy vấn của bạn. Hoặc có lẽ (trong trường hợp này) hãy thử GetElementsByTagName.

XmlNamespaceManager mgr = new XmlNamespaceManager(xmlDoc.NameTable); 
mgr.AddNamespace("x", 
    "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"); 
foreach (XmlNode node in xmlDoc.SelectNodes("//x:Template", mgr)) 
{ 
    Console.WriteLine("{0}: {1}", node.Name, node.InnerText); 
} 

Hoặc:

foreach (XmlNode node in xmlDoc.GetElementsByTagName("Template")) 
{ 
    Console.WriteLine("{0}: {1}", node.Name, node.InnerText); 
} 
+0

Tôi không biết bạn phải chọn một bí danh tùy ý để giải quyết đúng các truy vấn XPath theo cách này! Cảm ơn thông tin Marc. –

4

đây biểu xpath của bạn đòi hỏi một độ phân giải không gian tên. bạn phải instanciate một XmlNamespaceManager và sử dụng nó trong SelectNodes của bạn.

mẫu này nên làm việc

public static void TestXPath() 
    { 
     string xmlText = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"; 
     xmlText += "<Properties xmlns=\"http://schemas.openxmlformats.org/officeDocument/2006/extended-properties\" xmlns:vt=\"http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes\">"; 
     xmlText += "<Template>Normal</Template> <TotalTime>1</TotalTime> <Pages>1</Pages> <Words>6</Words>"; 
     xmlText += "</Properties>"; 

     XmlDocument xmlDoc = new XmlDocument(); 
     xmlDoc.Load(new System.IO.StringReader(xmlText)); 

     XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); 
     nsmgr.AddNamespace("res", "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"); 

     foreach (XmlNode node in xmlDoc.SelectNodes("//res:Template", nsmgr)) 
     { 
      Console.WriteLine("{0}: {1}", node.Name, node.InnerText); 
     } 
    } 

bạn cũng có thể nhận được không gian tên mặc định bằng cách sử dụng và viết

string s = xmlDoc.DocumentElement.GetNamespaceOfPrefix(""); 
nsmgr.AddNamespace("ns", s); 
2

Tại sao bạn cần không gian tên ở đây không? chỉ cần loại bỏ những hình ảnh này

xmlns=\"http://schemas.openxmlformats.org/officeDocument/2006/extended-properties\" 
xmlns:vt=\"http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes\" 

và lựa chọn của bạn sẽ hoạt động.

Các vấn đề liên quan