将特定的XML片段复制到基于Attribute的字符串

时间:2014-09-13 00:34:33

标签: c# xml linq-to-xml

我在XIB文件中有以下内容

<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,并且整个xib文件已经加载到XDocument中。

有没有办法可以抓住具有相应ID的特定按钮(带孩子)? SelectSingleNode看起来很合适,但每次传入按钮的属性ID时都会返回null。

2 个答案:

答案 0 :(得分: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);
}

试试吧

答案 1 :(得分:0)

至少有3种不同的方法可以通过<button>属性获取特定的id。您可以将XDocument结合使用LINQ to 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进行测试时,所有工作都可以获得特定的按钮元素标记。