1

I'm trying to modify an attribute of an xml node. However selecting the attribute is appearing to be a bit more confusing than i thought. Here is my code...

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

What i don't understand is why i have to define the entire xml tree when selecting the attribute within the selected element. Why can't i write the code like this?

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

For anyone who is trying to help me out, here is the xml file...

My ultimate goal: How do i select the Family Node and change the attribute isHidden to true then copy the new Family node with it's modified data to the clipboard?

I do know when i copy to the clipboard i would use something like 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>

2 Answers 2

1

Have you tried:

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

Note that it doen't start with a slash /

Sign up to request clarification or add additional context in comments.

Comments

0

Try this

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";
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.