Note that there are some explanatory texts on larger screens.

plurals
  1. POParse and access XML without Linq
    text
    copied!<p>i'm using a platform that does allow XML, but not Linq, so I will have to parse the XML without XElement and XDocument, so what should I use instead of these two? I also found a code, if someone could explain me how to convert it</p> <pre><code>// parse the document (this is your doc but I've made the xml parseable) var doc = XDocument.Parse(@"&lt;chars&gt; &lt;character name=""MyChar1""&gt; &lt;skill1 type=""attack"" damage=""30""&gt; description of skill1 &lt;name&gt;Skill name&lt;/name&gt; &lt;class1 type=""The Class Type""&gt;&lt;/class1&gt; &lt;class2 type=""The Class Type 2""&gt;&lt;/class2&gt; &lt;/skill1&gt; &lt;/character&gt; &lt;character name=""MyChar2""&gt; &lt;skill1 type=""attack"" damage=""30""&gt;&lt;/skill1&gt; &lt;/character&gt; &lt;/chars&gt;"); // Access a skill1 type(attribute) where the name(attribute) is "MyChar" // this is pretty easy with LINQ. We first get all descendant nodes of type "character" var skillWhereNameIsMyChar1 = doc.Descendants("character") // then take the single one with an attribute named "value" .Single(ch =&gt; ch.Attribute("name") != null &amp;&amp; ch.Attribute("name").Value == "MyChar1") // and take that element's child element of type skill1 .Element("skill1"); // this will print &lt;skill1 ... /skill1&gt;. However, this is an XElement object, not a string // so you can continue to access inner text, attributes, children etc. Console.WriteLine(skillWhereNameIsMyChar1); // 2. Access the description of skill1 where name(att) is "MyChar1" // this is tricky because the description text is just floating among other tags // if description were wrapped in &lt;description&gt;&lt;/description&gt;, this would be simply // var description = skillWhereNameIsMyChar1.Element("description").Value; // here's the hacky way I found to get it in the current xml: // first get the full value (inner text) of the skill node (includes "Skill Name") var fullValue = skillWhereNameIsMyChar1.Value; // then get the concatenated full values of all child nodes (= "Skill Name") var innerValues = string.Join("", skillWhereNameIsMyChar1.Elements().Select(e =&gt; e.Value)); // get the description by dropping off the trailing characters that are actually inner values // by limiting the length to the full length - the length of the non-description characters var description = fullValue.Substring(0, length: fullValue.Length - innerValues.Length); Console.WriteLine(description); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload