2012-12-30 134 views
2

这里多个XML子节点是我的XML文件:如何选择具有类似名称

<hashtable> 
    <entry> 
    <string>krishna.com</string> 
    <hashtable> 
     <entry> 
     <string>status</string> 
     <string>available</string> 
     </entry> 
     <entry> 
     <string>classkey</string> 
     <string>domcno</string> 
     </entry> 
    </hashtable> 
    </entry> 
    <entry> 
    <string>krishna.net</string> 
    <hashtable> 
     <entry> 
     <string>status</string> 
     <string>regthroughothers</string> 
     </entry> 
     <entry> 
     <string>classkey</string> 
     <string>dotnet</string> 
     </entry> 
    </hashtable> 
    </entry> 
</hashtable> 

我想找到状态可用(节点:哈希表/项/哈希表/项/串)由krishna.com (节点:hashtable/entry/string)。

这里我的困难是有节点像string & entry很多相似的名字,所以我怎么能检查是否status可用域krishna.com & krishna.net通过检查status(字符串节点)和available(串节点)。

回答

2

,你可以尝试这样的事情

XmlDocument xmlDoc = new XmlDocument(); 
xmlDoc.LoadXml("Path to your .xml file"); 
XmlNodeList nodeList = xmlDoc.SelectNodes("//entry/string"); 

现在你可以编写自己的代码来获得在您的节点列表中的信息;

0

您可以使用LINQ to XML来搜索满足指定标准的节点。 这是选择的第一级包含一个“状态”,“入门”节点及其子“哈希表”节点下,如你的榜样的结构“可用”子节点的例子:

XDocument doc; 
using (FileStream fs = new FileStream("XMLFile1.xml", FileMode.Open)) 
{ 
    doc = XDocument.Load(fs); 
} 
string[] elms = doc.Element("hashtable").Elements("entry").Where(elm => 
      elm.Element("hashtable").Element("entry").Elements("string").First().Value == "status" && 
      elm.Element("hashtable").Element("entry").Elements("string").Skip(1).First().Value == "available") 
     .Select(elm => elm.Element("string").Value).ToArray(); 

如果您文件结构可以是不同的,你提供的例子,你需要修改查询相应

0

您可能需要类似:

//hashtable/entry/string[1][text() = 'krishna.com']"/../hastable/entry/string[1][text() = 'status']"/../string[2][text() = 'available']

未经测试,但几件事情不E:

  • //装置开始在任一节点
  • A/B指根据元件元件B A
  • A/B[1]指根据元件的第一元件B A
  • A[text() = 'foo']意味着元素A包含foo的作为文本值
  • ..意味着父节点

如果这不起作用,您可以首先找到krishna.com的hashtable,然后在entry节点下的第1位找到包含值“status”的string节点,然后确保只有该节点的同级节点为string节点是包含“可用”的另一个string节点。

1

这是您的XPath解决方案。

using System; 
using System.Xml.Linq; 
using System.Xml.XPath; 

static string GetStatus(XDocument doc, string nodeName) 
{ 
    //The first xpath translates to: "Select the <hashtable> element that immediately 
    // follows the <string> element with a value of nodeName." 
    //The second xpath selects the <string> element that immediately follows another <string> 
    // element with a value of 'status'. This selected element is a descendant of the 
    // <hashtable> element that was selected first. 
    XElement status = doc.XPathSelectElement("//hashtable[preceding-sibling::string = '" + nodeName + "']") 
         .XPathSelectElement("descendant::string[preceding-sibling::string = 'status']"); 
    return status.Value; 
} 

相当自我解释使用此:

XDocument doc = XDocument.Load("File_path_to_XML.xml"); 
string status = GetStatus(doc, "krishna.com"); 
// status == "available" 
string status2 = GetStatus(doc, "krishna.net"); 
// status2 == "regthroughothers" 
+0

+1,很高兴看到有人使用“前同胞”。 –

0

以下是我认为:

XmlDocument xmlDoc = new XmlDocument(); 
xmlDoc.Load("your_xml_file.xml"); 
XmlElement root = xmlDoc.DocumentElement; 
string query = "child::entry/child::string[following-sibling::*/descendant::string[text()='available']]"; 
XmlNodeList nodes = root.SelectNodes(query); 
foreach (XmlNode node in nodes) 
{ 
    Console.WriteLine(node.InnerText); 
} 
//Result 
krishna.com 

它会显示域的名称是 “可用”。
在查询字符串中,“child :: entry”将选择称为“entry”的root子节点。 (不是后代)。所以路径是“/ hashtable/entry”。
接下来,对于每个“入口”节点,它将选择包含域名的“字符串”子节点。在这一步中,它使用一个表达式来选择“可用”的onle。现在路径变成了“/ hashtable/entry/string”。在表达式中,首先它将选择所有以下兄弟,并且在您的xml中,只有“散列表”符合条件。最后,对于以下所有兄弟姐妹,我们检查它的后代,如果后代的名字是“字符串”,并且其内部文本是“可用的”,那么将选择当前的域节点。
希望它有帮助。