Xml C# Find a Node Value Based on Another Node -
i need finding attributes of position if subtypename has text equals "weight"
the problem is, there multiple myobjectbuilder_entitybase elements (shown below) in xml file may or may not meet these requirements, have same structure.
the attributes x y , z stored in 3 respective list variables each meets conditions.
<myobjectbuilder_entitybase xsi:type="myobjectbuilder_cubegrid"> <entityid>173933426524952854</entityid> <persistentflags>castshadows inscene</persistentflags> <positionandorientation> <position x="32.206989288330078" y="28.401615142822266" z="11.562240600585937" /> <forward x="0.323335081" y="-0.00425125659" z="-0.946275" /> <up x="-0.9462663" y="-0.007667198" z="-0.32329765" /> </positionandorientation> <gridsizeenum>small</gridsizeenum> <cubeblocks> <myobjectbuilder_cubeblock xsi:type="myobjectbuilder_cubeblock"> <subtypename>weight</subtypename> <entityid>173933426524952855</entityid> <min x="0" y="0" z="0" /> <blockorientation forward="forward" up="up" /> <colormaskhsv x="0" y="-1" z="0" /> <sharemode>none</sharemode> <deformationratio>0</deformationratio> </myobjectbuilder_cubeblock> </cubeblocks> <isstatic>false</isstatic> <skeleton /> <linearvelocity x="0" y="0" z="0" /> <angularvelocity x="0" y="0" z="0" /> <xmirroxplane xsi:nil="true" /> <ymirroxplane xsi:nil="true" /> <zmirroxplane xsi:nil="true" /> <blockgroups /> <handbrake>false</handbrake> <displayname>grid 2854</displayname> <destructibleblocks>true</destructibleblocks> <createphysics>true</createphysics> <enablesmalltolargeconnections>true</enablesmalltolargeconnections> </myobjectbuilder_entitybase>
code have start is:
private void button3_click(object sender, eventargs e) { string choice; switch (combobox1.selectedindex) { case 0: //player choice = "player"; break; case 1://weight choice = "weight"; break; case 2://catapult choice = "catapulthead"; break; case 3://ropething choice = "ropeendingsmall"; break; case 4://ropethingbig choice = "ropereleasesmall"; break; case 5://turncross choice = "turncrosssmall"; break; default: choice = "blah"; break; } random rand = new random(); xmldocument xmldoc = new xmldocument(); xmldoc.load(pathtree); xmlreader xmlreader = xmlreader.create(pathtree); xmlnamespacemanager nsmanager = new xmlnamespacemanager(xmldoc.nametable); nsmanager.addnamespace("xsi", "http://www.w3.org/2001/xmlschema-instance"); list<double> x = new list<double>(); list<double> y = new list<double>(); list<double> z = new list<double>(); ////myobjectbuilder_sector/sectorobjects/myobjectbuilder_entitybase[@xsi:type = 'myobjectbuilder_cubegrid']/cubeblocks/myobjectbuilder_cubeblock[@xsi:type = 'myobjectbuilder_cubeblock']/subtypename //xmlreader. while (xmlreader.read()) { switch (combobox1.selectedindex) { case 0: //player x.add( convert.todouble(xmldoc.selectsinglenode("//myobjectbuilder_sector/sectorobjects/myobjectbuilder_entitybase[@xsi:type = 'myobjectbuilder_character']/positionandorientation/position", nsmanager).attributes["x"].value) ); y.add( convert.todouble(xmldoc.selectsinglenode("//myobjectbuilder_sector/sectorobjects/myobjectbuilder_entitybase[@xsi:type = 'myobjectbuilder_character']/positionandorientation/position", nsmanager).attributes["y"].value) ); z.add( convert.todouble(xmldoc.selectsinglenode("//myobjectbuilder_sector/sectorobjects/myobjectbuilder_entitybase[@xsi:type = 'myobjectbuilder_character']/positionandorientation/position", nsmanager).attributes["z"].value) ); break; case 1://weight //xmldoc.selectnodes() //x.add(from type in xmldoc.selectsinglenode("//myobjectbuilder_sector/sectorobjects/myobjectbuilder_entitybase[@xsi:type = 'myobjectbuilder_character']/positionandorientation/position", nsmanager)); //select x.value('description[1]','varchar(max)') 'description' xmldoc.selectnodes('//incomeitem[typeid=29]') i(x) break; case 2://catapult break; case 3://ropething break; case 4://ropethingbig break; case 5://turncross break; default: break; }
in case of subtype equaling "player," can retrieve because there one. rest hard because there more one.
while i'm @ it, need delete element myobjectbuilder if meets conditions.
had strip namespaces load, should work, works locally anyhow
var xml = "<myobjectbuilder_entitybase>" + "<entityid>173933426524952854</entityid>" + "<persistentflags>castshadows inscene</persistentflags>" + "<positionandorientation>" + "<position x=\"32.206989288330078\" y=\"28.401615142822266\" z=\"11.562240600585937\" />" + "<forward x=\"0.323335081\" y=\"-0.00425125659\" z=\"-0.946275\" />" + "<up x=\"-0.9462663\" y=\"-0.007667198\" z=\"-0.32329765\" />" + "</positionandorientation>" + "<gridsizeenum>small</gridsizeenum>" + "<cubeblocks>" + "<myobjectbuilder_cubeblock>" + "<subtypename>weight</subtypename>" + "<entityid>173933426524952855</entityid>" + "<min x=\"0\" y=\"0\" z=\"0\" />" + "<blockorientation forward=\"forward\" up=\"up\" />" + "<colormaskhsv x=\"0\" y=\"-1\" z=\"0\" />" + "<sharemode>none</sharemode>" + "<deformationratio>0</deformationratio>" + "</myobjectbuilder_cubeblock>" + "</cubeblocks>" + "<isstatic>false</isstatic>" + "<skeleton />" + "<linearvelocity x=\"0\" y=\"0\" z=\"0\" />" + "<angularvelocity x=\"0\" y=\"0\" z=\"0\" />" + "<xmirroxplane />" + "<ymirroxplane />" + "<zmirroxplane />" + "<blockgroups />" + "<handbrake>false</handbrake>" + "<displayname>grid 2854</displayname>" + "<destructibleblocks>true</destructibleblocks>" + "<createphysics>true</createphysics>" + "<enablesmalltolargeconnections>true</enablesmalltolargeconnections>" + "</myobjectbuilder_entitybase>"; var doc = new xmldocument(); doc.loadxml(xml); var xlist = new list<string>(); var ylist = new list<string>(); var zlist = new list<string>(); var subtypename = doc.selectsinglenode("//subtypename"); if (subtypename.innertext == "weight") { var position = doc.selectsinglenode("//position"); xlist.add(position.attributes["x"].value); ylist.add(position.attributes["y"].value); zlist.add(position.attributes["z"].value); }
Comments
Post a Comment