如何修改xml节点的属性c#

时间:2016-04-10 14:09:58

标签: c# xml

我正在尝试修改xml节点的属性。然而,选择属性似乎比我想象的更混乱。这是我的代码......

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

我不明白为什么在选择所选元素中的属性时必须定义整个xml树。为什么我不能写这样的代码?

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

对于任何想要帮助我的人,这里是xml文件......

  

我的最终目标:如何选择家庭节点并更改    isHidden 属性 true 然后使用复制新的Family节点   它将数据修改为剪贴板?

我知道当我复制到剪贴板时,我会使用像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>

1 个答案:

答案 0 :(得分:1)

你试过了吗?

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

请注意,它不能以斜杠/

开头