2013-01-02 66 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; 
} 

之類的事情。