c# - Load specific XML attributes/values -


i've been trying 2 days understand linq xml in c# but, i'm messed 'cause never saw tutorial other one, mean different , since i'm beginner in c#, , need c# in serverside of game wondering how to

  1. access skill1 type(attribute) name(attribute) "mychar"
  2. access description of skill1 name(att) "mychar1"

<chars>     <character name="mychar1">         <skill1 type="attack" damage="30">             description of skill1             <name>skill name</name>             <class1 type="the class type"></class1>             <class2 type="the class type 2"></class2>         </skill1>          //almost same thing till skill4             <skill2>...</skill4>     <character name="mychar2">         <skill1 type="attack" damage="30"></skill1>         <skill2>....</skill4>     </character> </chars> 

try code:

// parse document (this doc i've made xml parseable) var doc = xdocument.parse(@"<chars>         <character name=""mychar1"">         <skill1 type=""attack"" damage=""30"">             description of skill1             <name>skill name</name>             <class1 type=""the class type""></class1>             <class2 type=""the class type 2""></class2>         </skill1>         </character>     <character name=""mychar2"">         <skill1 type=""attack"" damage=""30""></skill1>     </character> </chars>");  // access skill1 type(attribute) name(attribute) "mychar"  // pretty easy linq. first descendant nodes of type "character" var skillwherenameismychar1 = doc.descendants("character")     // take single 1 attribute named "value"     .single(ch => ch.attribute("name") != null && ch.attribute("name").value == "mychar1")     // , take element's child element of type skill1      .element("skill1"); // print <skill1 ... /skill1>. however, xelement object, not string // can continue access inner text, attributes, children etc. console.writeline(skillwherenameismychar1);  // 2. access description of skill1 name(att) "mychar1" // tricky because description text floating among other tags // if description wrapped in <description></description>, // var description = skillwherenameismychar1.element("description").value; // here's hacky way found in current xml:  // first full value (inner text) of skill node (includes "skill name") var fullvalue = skillwherenameismychar1.value; // concatenated full values of child nodes (= "skill name") var innervalues = string.join("", skillwherenameismychar1.elements().select(e => e.value)); // description dropping off trailing characters inner values // limiting length full length - length of non-description characters var description = fullvalue.substring(0, length: fullvalue.length - innervalues.length); console.writeline(description); 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -