2016-04-10 15 views
1

Bir xml düğümünün özniteliğini değiştirmeye çalışıyorum. Ancak özniteliği seçmek düşündüğümden biraz daha kafa karıştırıcı görünmektedir. İşte benim kod ... ben seçilmiş öğesi içinde niteliğini seçerken tüm xml ağacını tanımlamak zorunda nedenbir xml düğümünün özniteliği nasıl değiştirilir C#

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

Ne anlamıyorum olmasıdır. Kodu neden böyle yazamıyorum?

bana yardım etmeye çalışıyor herkes için
element = root.SelectSingleNode "/nexus/category/node[@className='Family']" 
attr = element.SelectSingleNode "/node/attributeblocks/attributeblock/attribute[@key='isHidden']" 

, burada

Benim nihai hedefi

... xml dosyasıdır: i Aile Düğüm seçip nasıl özelliğini değiştirmek isHidden için true ile yeni Aile düğümünü kopyalayın ve değiştirilmiş veriyi panele kopyalayın.

i

<?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> 

cevap

1

element.OuterXML gibi bir şey kullanmak istiyorsunuz panoya sen denediniz kopyalarken anlarım: Bir eğik çizgi / ile başlamak doent olduğunu

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

Not

0

Bunu deneyin

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"; 
     } 
    } 
} 
İlgili konular