2014-09-13 69 views
1

我已在从XIB文件复制一条特定的XML将字符串基于属性

<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center"  contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="OGN-yD-hzb"> 
       <rect key="frame" x="4" y="21" width="36" height="36"/> 
       <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> 
       <constraints> 
        <constraint firstAttribute="height" constant="36" id="Sgc-0Z-H82"/> 
        <constraint firstAttribute="width" constant="36" id="WLV-12-NHf"/> 
       </constraints> 
       <fontDescription key="fontDescription" type="boldSystem" pointSize="15"/> 
       <state key="normal" title="fdf"> 
        <color key="titleColor" red="0.19607843459999999" green="0.30980393290000002" blue="0.52156865600000002" alpha="1" colorSpace="calibratedRGB"/> 
        <color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/> 
       </state> 
       <state key="highlighted"> 
        <color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/> 
       </state> 
      </button> 
      <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="abV-ij-rke"> 
       <rect key="frame" x="276" y="21" width="36" height="36"/> 
       <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> 
       <constraints> 
        <constraint firstAttribute="width" constant="36" id="ps5-Pb-Ebc"/> 
       </constraints> 
       <fontDescription key="fontDescription" type="boldSystem" pointSize="15"/> 
       <state key="normal" title="Button"> 
        <color key="titleColor" red="0.19607843459999999" green="0.30980393290000002" blue="0.52156865600000002" alpha="1" colorSpace="calibratedRGB"/> 
        <color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/> 
       </state> 
       <state key="highlighted"> 
        <color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/> 
       </state> 
      </button> 

我已经从连接和整个厦门国际银行文件上分离的DestinationIds以下已经加载到XDocument中。

有没有一种方法可以抓住具有相应ID的特定按钮(带孩子)? SelectSingleNode看起来是这样,但是每次我传入按钮的属性ID时,它都会返回null。

+2

显示您在SelectSingleNode中使用的XPath。 – Hassan 2014-09-13 00:46:54

回答

0

至少有3种不同的方法来获得特定<button>通过它的id属性。您可以使用XDocument与任何LINQ组合,以XML标准方法或XPathSelectElement()扩展,像这样:

var doc = XDocument.Parse(xml); 
var xpathResult = doc.XPathSelectElement("//button[@id='abV-ij-rke']") 
        .ToString(); 
var queryResult = doc.Descendants("button") 
        .First(o => (string)o.Attribute("id") == "abV-ij-rke") 
        .ToString(); 

或者使用旧的XML API,XmlDocument,正如你所提到的有关SelectSingleNode()

var doc = new XmlDocument(); 
doc.LoadXml(xml); 
var result = doc.DocumentElement 
       .SelectSingleNode("//button[@id='abV-ij-rke']") 
       .OuterXml; 

都能正常运作,以在针对您发布的XML进行测试时获取特定的按钮元素标记。

+0

谢谢 - 正是我期待的:) – Nodoid 2014-09-13 15:23:11

0
var buttons = doc.GetElementsByTagName("button"); 
foreach (var button in buttons) 
{ 
    Console.WriteLine(((XmlElement) button).GetAttribute("Id")); 
    Console.WriteLine(((XmlElement) button).InnerText); 
} 
var rect= doc.GetElementsByTagName("rect "); 
foreach (var button in buttons) 
{ 
    Console.WriteLine(((XmlElement) button).GetAttribute("ID")); 
    Console.WriteLine(((XmlElement) button).InnerText); 
    Console.WriteLine(((XmlElement)rect).ParentNode); 
} 

尝试