C#通过xpath从dom获取文本值
using System.Xml; using System.Xml.XPath; ... //create initial DOM XmlDocument xmlDocument = new XmlDocument(); /// <TextDefinitions> /// <TextDefinition name="DefaultDemographicText"> /// <Text lang="en-UK">Thanks for taking our survey.</Text> /// <Text lang="fr-FR">Merci pour prendre notre apercu.</Text> /// </TextDefinition> /// <TextDefinition name="ExitText"> /// <Text lang="en-UK">Goodbye</Text> /// <Text lang="fr-FR">Au revoir </Text> /// </TextDefinition> /// </TextDefinitions> xmlDocument.LoadXml("<TextDefinitions> <TextDefinition name='DefaultDemographicText'> <Text lang='en-UK'>Thanks for taking our survey.</Text> <Text lang='fr-FR'>Merci pour prendre notre apercu.</Text> </TextDefinition> <TextDefinition name='ExitText'> <Text lang='en-UK'>Goodbye</Text> <Text lang='fr-FR'>Au revoir </Text> </TextDefinition></TextDefinitions>"); XPathNavigator xPathNavigator = xmlDocument.CreateNavigator(); ... private string GetTextFromDOM(string textName,string lang) { string query = "string(/TextDefinitions/TextDefinition[@name='"+textName+"']/Text[@lang = '"+lang+"'])"; string text = (string) xPathNavigator.Evaluate(query); if(verbose) { Util.write("GetTextFromDOM(\""+textName+"\", \""+lang+"\"): \""+text+"\""); } return text; } }