Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The code sample below might be of some help for you.</p> <p>Instead of declaring a variable "name" and "value" in XML, use XML serialization provided by .NET to parse the XML into appropriate variables. Feel free to remove the "name" from your XML altogether, or turn it into a device for clearly expressing the purpose of the value. I have updated your XML to use elements (more readable) instead of attributes below:</p> <p><strong>XML Sample</strong></p> <pre><code>&lt;Test&gt; &lt;Params&gt; &lt;Param&gt; &lt;Name&gt;Meaningful Parameter Name&lt;/Name&gt; &lt;Value&gt;1&lt;/Value&gt; &lt;/Param&gt; &lt;Param&gt; &lt;Name&gt;Meaningful Parameter Name2&lt;/Name&gt; &lt;Value&gt;0&lt;/Value&gt; &lt;/Param&gt; &lt;/Params&gt; &lt;/Test&gt; </code></pre> <p></p> <p>It is in the code that we assign XML elements to variables, using xml serialization. As you can see, the Param class will contain a string name and a boolean value, which will be read from the XML elements "Name" and "Value". This allows you to change your variable names without having to worry about updating the XML to reflect them. If you changed your mind and wanted to rename "Value" in the XML to something different, you would just need to update the code in one place <em>[XmlElement("Value")]</em> with the new name defined in the XML. Simple! :)</p> <p>Good luck!</p> <p><strong>Code Example</strong></p> <pre><code>using System; using System.Collections.Generic; using System.IO; using System.Xml.Linq; using System.Xml.Serialization; namespace ConsoleApplication1 { public class Program { private static void Main(string[] args) { string testXML = @"&lt;Test&gt; &lt;Params&gt; &lt;Param&gt; &lt;Name&gt;Meaningful Parameter Name&lt;/Name&gt; &lt;Value&gt;1&lt;/Value&gt; &lt;/Param&gt; &lt;Param&gt; &lt;Name&gt;Meaningful Parameter Name2&lt;/Name&gt; &lt;Value&gt;0&lt;/Value&gt; &lt;/Param&gt; &lt;/Params&gt; &lt;/Test&gt;"; XDocument doc = XDocument.Parse(testXML); XmlSerializer serializer = new XmlSerializer(typeof (Test)); Test testDeserialized = (Test) serializer.Deserialize(doc.CreateReader()); foreach (Param param in testDeserialized.Params) { Console.WriteLine("Name: " + param.Name + ", Value: " + param.Value); } Console.ReadLine(); } } [XmlRoot] public class Test { [XmlArray("Params")] [XmlArrayItem("Param", typeof (Param))] public Param[] Params { get; set; } } public class Param { [XmlElement("Name")] public string Name { get; set; } [XmlElement("Value")] public bool Value { get; set; } } } </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