System.Xml.Linq匹配Xpath的替代方法

本文关键字:方法 Xpath Xml Linq 匹配 System | 更新日期: 2023-09-27 17:50:19

我使用System.Xml.Linq;从xml文档匹配Xpath。XElementSaveOptions均由System.Xml.Linq;得到。

           XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
            nsmgr.AddNamespace("ns", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-02-03T16:54:46");
            XElement docRfsid = XElement.Parse(content);
            //if (docRfsid.XPathSelectElement("//ns:RFSID", nsmgr).Value != null)
            if (Regex.Match(docRfsid.ToString(), "RFSID", RegexOptions.IgnoreCase).Success)
            {
                projData.RfsId = docRfsid.XPathSelectElement("//ns:RFSID", nsmgr).Value.ToString();
            }
            XElement doc_Financial = XElement.Parse(content);
            string resultFinancial = doc_Financial.XPathSelectElement("//ns:Financial", nsmgr).ToString(SaveOptions.DisableFormatting);

我只是想删除System.Xml.Linq; dll,因为我只能使用。net框架2.0。有没有其他的替代方案,我可以使用System.Xml.Linq; .

System.Xml.Linq匹配Xpath的替代方法

是。使用System.Xml。XmlDocument,特别是其上的SelectNodes()方法、DocumentElement属性或任何XmlElement实例。该方法接受XPath并返回匹配的xmlelement列表(无论它们是节点(XmlNode)还是属性(XmlAttribute))。这是基于旧的COM XmlDocument对象,早在框架的1.1版就可以使用。

System.Xml

之类的
XmlDocument doc = new XmlDocument();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ns", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-02-03T16:54:46");
XmlNode financialNode = doc.DocumentElement.SelectNode("ns:Financial",nsmgr);
strring resultFinancial = null;
if (financialNode != null)
{
  resultFinancial = financialNode.InnerText;
}

之类的