Note that there are some explanatory texts on larger screens.

plurals
  1. POXML import to C# Object
    text
    copied!<p>I have an XML file with the follow in it:</p> <pre><code>&lt;tooltip&gt; &lt;text&gt;My Tool Tip&lt;/text&gt; &lt;color&gt;#000000&lt;/color&gt; &lt;crosshairs&gt; &lt;width&gt;3&lt;/width&gt; &lt;color&gt;green&lt;/color&gt; &lt;padding&gt;5px&lt;/padding&gt; &lt;/crosshairs&gt; &lt;crosshairs&gt; &lt;width&gt;3&lt;/width&gt; &lt;color&gt;blue&lt;/color&gt; &lt;/crosshairs&gt; &lt;/tooltip&gt; </code></pre> <p>I wish to load the "crosshairs" elements into a C# object - the object is then serialized out to a JSON file:</p> <pre><code>ToolTipClass toolTip = new ToolTipClass(xmlDoc); JavaScriptSerializer s = new JavaScriptSerializer(); string aJSON = s.Serialize(toolTip); // aJSON is now written out to file. </code></pre> <p>I do have a "toolTip" class with some public variables which are set by default in the constructor. The constuctor also reads the XML file in and overwrites the public variables with the value in the XML (e.g. is written into "public string text;", but the "crosshairs" part of the tool tip can contain either 1 or several elements - I don't want to define a class for the crosshairs as the tags within the crosshairs can be more than just the 2 defined above (width, color). eg (padding, margin, fontSize etc.)</p> <p>the JSon output would look something like this:</p> <pre><code>tooltip: { text: "My Tool Tip", color: "#000000", crosshairs: [{ width: 3, color: 'green', padding: '5px' }, { width: 3, color: 'blue' }] } </code></pre> <p>What I need to know is How do I load the crosshairs elements into an object that isn't predefiend?</p> <p>I have looked at: <a href="http://danielwylie.me/blog/2010/04/26/c-convert-xml-to-an-object-or-list-of-an-object/?Focus=Yes" rel="nofollow">http://danielwylie.me/blog/2010/04/26/c-convert-xml-to-an-object-or-list-of-an-object/?Focus=Yes</a> but this uses a "person" class which is not what I wanted.</p> <p>Many thanks for any help/pointers.</p> <hr> <p>Extra: ToolTip Class:</p> <pre><code>public class ToolTip { public string backgroundColor = "rgba(255, 255, 255, .80)"; public string borderColor = "#764D9B"; public int borderRadius = 5; public int borderWidth = 1; //public string crosshairs = null; //public List&lt;object&gt; crosshairs = new List&lt;object&gt;(); public Boolean enabled = true; //formatter: ; public Boolean shadow = true; public Boolean shared = false; public float snap = 10; public HCCSS style = new HCCSS(); public string text = "[undefined]"; public string color = "#000000"; public HCToolTip(XmlDocument xmlDoc) { text = findTagInXML_String(xmlDoc, "//tooltip/text", text); color = findTagInXML_String(xmlDoc, "//tooltip/color", color); //snip } static private string findTagInXML_String(XmlDocument xmlDoc, string tag, string defaultvalue) { return xmlDoc.SelectSingleNode(tag) == null || xmlDoc.SelectSingleNode(tag).InnerText == "null" ? defaultvalue : xmlDoc.SelectSingleNode(tag).InnerText; } } </code></pre> <hr> <h2>update / reply ## (not sure how to do replies below).</h2> <p>Thanks for the code and the link to the converter site. I've added in some code and sort of got it doing something, but I do have a few more problems.</p> <ol> <li><p>How do I get the XML data into the Crosshairs collection. I've currently got this in my toolTip constructor class:</p> <pre><code>Crosshairs c = new Crosshairs(); c.SetProperty("a",new {width="3", color="green"}); c.SetProperty("b", new { width = "3", color = "blue" }); crosshairs.Add(c); </code></pre></li> </ol> <p>I'm assuming where I've got the new width color is where I would want it to bring in the details from the XML file mentioned above.</p> <ol> <li><p>I've added in the Converter class but the output I'm now getting is something like:</p> <p>tooltip: { borderColor: "#F00" crosshairs: { List: [ { Value: { width: "3" color: "green" } Text: { width: "3" color: "blue" } } ] } enabled: true</p> <p>}</p></li> </ol> <p>I did change the example converter to have the following rows:</p> <pre><code>foreach (Crosshairs item in listType) { //Add each entry to the dictionary. Dictionary&lt;string, object&gt; listDict = new Dictionary&lt;string, object&gt;(); listDict.Add("Value", item.GetProperty("a")); listDict.Add("Text", item.GetProperty("b")); itemsList.Add(listDict); } result["List"] = itemsList; </code></pre> <p>As you can see it' doesn't look very generic as it uses types of "CrosshairsCollection", but I'm sort of guessing that I can change the "CrosshairsCollection" to a "GenericCollection" as it's only a dictionary - so #1 above still applies.</p> <ol> <li>The other problem is that not only the toolTip has this "crosshairs" class, but I do have other classes that are similar to crosshairs - eg style which I would like to have the same system.</li> </ol> <p>If anyone could help with #1 above - importing the data from XML - to generic class rather than a predefined class it would really help.</p> <p>Again - Many thanks for the help. Alan</p>
 

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