2013-01-02 69 views
0

我已经使用System.Xml.Linq;来匹配来自xml文档的Xpath。 XElementSaveOptions均得自System.Xml.Linq;System.Xml.Linq的替代方案与Xpath匹配

  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;

回答

1

是的。使用System.Xml.XmlDocument,特别是其上的SelectNodes()方法,DocumentElement属性或任何XmlElement实例。此方法接受XPath并返回匹配的XmlElements列表(无论它们是节点(XmlNode)还是属性(XmlAttribute))。这是基于旧的COM XmlDocument对象,并且早在版本1.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; 
} 

之类的事情。