可能重复:在XML中的一个节点下查找值
Sample XML File:
<Book>
<Page id="1">
<Head>Preface</Head>
<Body>
<Paragraph>
<Line>
<Word>
<Char>T</Char>
<Char>h</Char>
<Char>i</Char>
<Char>s</Char>
</Word>
<Word>is sample xml file.</Word>
</Line>
</Paragraph>
</Body>
</Page>
Note: 此处,要搜索的示例文本为'I' . 对于没有节点的选择Word,xPath表达式为:
string Word_Char_XPath="//CHAR[contains(translate(text(),'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ'),'I')]/..";
为了选择只有节点的单词,xPath表达式为:
string Word_XPath="//WORD[contains(translate(text(), abcdefghijklmnopqrstuvwxyz, ABCDEFGHIJKLMNOPQRSTUVWXYZ), 'I')]";
选择包含“i”的单词的代码
XmlDocument objXmlDoc = new XmlDocument();
XmlNodeList objXmlNodeList;
objXmlDoc.Load(sFilePath);
objXmlNodeList = objXmlDoc.SelectNodes(Word_Char_XPath+" or "+ Word_XPath);
Problem: 由'or'两个xpath表达式返回true或false,但是如果要在匹配条件(两个xpath)上选择所有单词节点,那么如何实现它?
1 回答
好吧,我've solved it by getting some help form Net. Where I'使用' or ' operator, use ' | '( Pipe Sign )运算符来组合两个XPath表达式的值 . 它将选择xml文件中与XPath表达式中的任一条件匹配的所有节点 . 快乐编程!