这是使用XmlDocument
and的解决方案SelectSingleNode
:
var xml = @"
<SplitterLayoutDataSet xmlns=""http://tempuri.org/SplitterLayoutDataSet.xsd"">
<SplitterLayout>
<SplitterName>mainSplitContainerControl</SplitterName>
<SplitterPosition>0.2213375796178344</SplitterPosition>
</SplitterLayout>
</SplitterLayoutDataSet>";
//Create XmlDocument
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
//Create NamespaceManager (for XPath)
var nsMgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsMgr.AddNamespace("dn", "http://tempuri.org/SplitterLayoutDataSet.xsd");
//Get inner text of SplitterLayout/SplitterPosition
var xPath = "//dn:SplitterLayout/dn:SplitterPosition";
Console.WriteLine(xmlDoc.SelectSingleNode(xPath, nsMgr).InnerText);
或者根据@AlexeiLevenkov 的建议,您可以NamespaceManager
通过使用以下 XPath 来消除:
var xPath = "//*[local-name()='SplitterPosition']";
Console.WriteLine(xmlDoc.SelectSingleNode(xPath).InnerText);
当然,你也可以使用XDocument
:
var xdoc = XDocument.Parse(xml);
XNamespace ns = @"http://tempuri.org/SplitterLayoutDataSet.xsd";
Console.WriteLine(xdoc.Descendants(ns + "SplitterPosition").First().Value);
或使用XDocument
and XPathSelectElement
:
var xdoc = XDocument.Parse(xml);
var xPath = "//*[local-name()='SplitterPosition']";
Console.WriteLine(xdoc.XPathSelectElement(xPath).Value);