2016-04-10 12 views
1

Ich versuche, ein Attribut eines XML-Knotens zu ändern. Die Auswahl des Attributs erscheint jedoch etwas verwirrender als ich dachte. Hier ist mein Code ...wie Attribut eines xml-Knotens zu ändern C#

element = root.SelectSingleNode "/nexus/category/node[@className='Family']" 
attr = element.SelectSingleNode "/nexus/category/node/attributeblocks/attributeblock/attribute[@key='isHidden']" 

Was ich nicht verstehe, ist, warum ich die gesamte XML-Struktur zu definieren, wenn das Attribut innerhalb des gewählten Elements auswählen. Warum kann ich den Code nicht so schreiben?

element = root.SelectSingleNode "/nexus/category/node[@className='Family']" 
attr = element.SelectSingleNode "/node/attributeblocks/attributeblock/attribute[@key='isHidden']" 

Für alle, die mir zu helfen versucht, aus, hier ist die XML-Datei ...

Mein Ziel: Wie die Familie Knoten i auswählen und das Attribut isHidden ändern zu true Kopieren Sie dann den neuen Familienknoten mit es ist die geänderten Daten in die Zwischenablage?

Ich weiß, wenn ich in die Zwischenablage kopieren ich so etwas wie element.OuterXML

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE nexus> 
<nexus version="1.0"> 
    <category name="Container"> 
     <node className="Group Box" virtualName="" image=":/images/icon.ico" enabled="True" backgroundColor="RGBA(255,255,0,255)" isContainer="True"> 
      <viewblocks> 
       <rect x="0.0" y="0.0" width="350" height="250"/> 
      </viewblocks> 
     </node> 
    </category> 
    <category name="General"> 
     <node className="Family" virtualName="Family" image=":/images/icon.ico" enabled="True" backgroundColor="RGBA(0,255,0,255)"> 
      <viewblocks> 
       <rect x="0.0" y="0.0" width="150" height="120"/> 
      </viewblocks> 
      <inputs> 
       <item name="Input 01" type="Kids" limit="2"/> 
       <item name="Input 02" type="People"/> 
      </inputs> 
      <outputs> 
       <item name="Output 01" type="Kids" limit="5"/> 
       <item name="Output 02" type="People" limit="23"/> 
       <item name="Output 03" type="Cars"/> 
      </outputs> 
      <attributeblocks> 
       <attributeblock name = "Basic Properties"> 
        <attribute type="boolean" key="isHidden" label="Is Hidden" value="False"/> 
        <attribute type="string" key="nickname" label="Nickname" value="Candy Bar"/> 
       </attributeblock> 
       <attributeblock name = "User Properties"> 
        <attribute type="float" key="average" label="Average" value="20.5" minValue="0.0" maxValue="100.0"/> 
        <attribute type="integer" key="integervalue" label="Integer Value" value="205" minValue="20" maxValue="1000"/> 
       </attributeblock> 
       <attributeblock name="Cool Stuff"> 
        <attribute type="color" key="colorA" label="Color" value="RGBA(0,255,0,255)"/> 
        <attribute type="color" key="colorB" label="Color" value="RGBA(255,0,255,255)"/> 
        <attribute type="color" key="colorC" label="Color" value="RGBA(0,100,200,80)"/> 
       </attributeblock> 
      </attributeblocks> 
     </node> 
     <node className="Cops" virtualName="Cops" image=":/images/icon.ico" enabled="True" backgroundColor="RGBA(0,255,0,255)"> 
      <viewblocks> 
       <rect x="0.0" y="0.0" width="100" height="120"/> 
      </viewblocks> 
      <inputs> 
       <item name="Input 01" type="People" limit="3"/> 
       <item name="Input 02" type="Cars" limit="2"/> 
       <item name="Input 03" type="Kids"/> 
      </inputs> 
      <outputs> 
       <item name="Output 01" type="People" limit="0"/> 
       <item name="Output 02" type="Cars" limit="2"/> 
      </outputs> 
      <attributeblocks> 
       <attributeblock name="Cool Stuff"> 
        <attribute type="dropdownlist" key="dropdownlist" label="DropDown List" value="Item 5" items="Item 1,Item 2,Item 3,Item 4,Item 5"/> 
        <attribute type="folderpath" key="folderpath" label="Folder Path" value=""/> 
        <attribute type="textbox" key="textbox" label="Text Box" value="Joe, Sam, Leslie, Kevin, Michael, Chris"/> 
        <attribute type="filepath" key="filepathopen" label="File Open Path" value="" filter="Files(*.png *.jpg *.jpeg)" method="open"/> 
        <attribute type="filepath" key="filepathsave" label="File Save Path" value="" filter="Files(*.png *.jpg *.jpeg)" method="save"/> 
       </attributeblock> 
       <attributeblock name = "User Properties"> 
        <attribute type="float" key="average" label="Average" value="20.5" minValue="0.0" maxValue="100.0"/> 
        <attribute type="integer" key="integervalue" label="Int Test" value="205" minValue="20" maxValue="1000"/> 
       </attributeblock> 
      </attributeblocks> 
     </node> 
    </category> 
    <category name="Extra"> 
     <node className="Schools" virtualName="Schools" image=":/images/icon.ico" enabled="True" backgroundColor="RGBA(0,255,0,255)"> 
      <viewblocks> 
       <rect x="0.0" y="0.0" width="50" height="60"/> 
      </viewblocks> 
      <inputs> 
       <item name="In" type="Kids" limit="2"/> 
      </inputs> 
      <outputs> 
       <item name="Out" type="Kids" limit="4"/> 
      </outputs> 
      <attributeblocks> 
       <attributeblock name = "Basic Properties"> 
        <attribute type="boolean" key="isHidden" label="Is Hidden" value="False"/> 
        <attribute type="string" key="nickname" label="Nickname" value="Candy Nick"/> 
       </attributeblock> 
       <attributeblock name = "User Properties"> 
        <attribute type="color" key="color1" label="Test Color" value="ff00ff"/> 
        <attribute type="string" key="nickname" label="Nickname" value="Stefan Hord"/> 
       </attributeblock> 
      </attributeblocks> 
     </node> 
     <node className="Workers" virtualName="Workers" image=":/images/icon.ico" enabled="True" backgroundColor="RGBA(0,255,0,255)"> 
      <viewblocks> 
       <rect x="0.0" y="0.0" width="150" height="100"/> 
      </viewblocks> 
      <inputs> 
       <item name="Input 01" type="Kids" limit="2"/> 
       <item name="Input 02" type="Cars"/> 
       <item name="Input 03" type="Peoples" limit="0"/> 
       <item name="Input 04" type="Peoples"/> 
      </inputs> 
      <outputs> 
       <item name="Output 01" type="Kids"/> 
       <item name="Output 02" type="Cars" limit="8"/> 
       <item name="Output 03" type="Cars" limit="4"/> 
       <item name="Output 04" type="Peoples"/> 
      </outputs> 
      <attributeblocks> 
       <attributeblock name = "User Properties"> 
        <attribute type="integer" key="integerid" label="Int Text" value="489" minValue="-10" maxValue="23456"/> 
        <attribute type="string" key="nickname" label="Nickname" value="Candy Bar"/> 
       </attributeblock> 
      </attributeblocks> 
     </node> 
    </category> 
</nexus> 

Antwort

1

Haben Sie versucht, verwenden würden: /

attr = element.SelectSingleNode "attributeblocks/attributeblock/attribute[@key='isHidden']"; 

Beachten Sie, dass es doen't beginnen mit einem Schrägstrich Geben Sie

0
ein
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 

namespace ConsoleApplication84 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      XDocument doc = XDocument.Load(FILENAME); 
      var family = doc.Descendants("node").Where(x => x.Attribute("className").Value == "Family"); 

      var hidden = family.Descendants("attribute").Where(x => x.Attribute("key").Value == "isHidden").FirstOrDefault(); 

      hidden.Attribute("value").Value = "True"; 
     } 
    } 
}