Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4535044
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:21:22+00:00 2026-05-21T14:21:22+00:00

I have an XML file with the follow in it: <tooltip> <text>My Tool Tip</text>

  • 0

I have an XML file with the follow in it:

<tooltip>
    <text>My Tool Tip</text>
    <color>#000000</color>
    <crosshairs>
        <width>3</width>
        <color>green</color>
        <padding>5px</padding>
    </crosshairs>
    <crosshairs>
        <width>3</width>
        <color>blue</color>
    </crosshairs>
</tooltip>

I wish to load the “crosshairs” elements into a C# object – the object is then serialized out to a JSON file:

ToolTipClass toolTip = new ToolTipClass(xmlDoc);
JavaScriptSerializer s = new JavaScriptSerializer();
string aJSON = s.Serialize(toolTip);
// aJSON is now written out to file.

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.)

the JSon output would look something like this:

tooltip: {
    text: "My Tool Tip",
    color: "#000000",
    crosshairs: [{
        width: 3,
        color: 'green',
        padding: '5px'
    }, {
        width: 3,
        color: 'blue'
    }]
}

What I need to know is How do I load the crosshairs elements into an object that isn’t predefiend?

I have looked at: http://danielwylie.me/blog/2010/04/26/c-convert-xml-to-an-object-or-list-of-an-object/?Focus=Yes
but this uses a “person” class which is not what I wanted.

Many thanks for any help/pointers.


Extra:
ToolTip Class:

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<object> crosshairs = new List<object>();
    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;
}
}

update / reply ## (not sure how to do replies below).

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.

  1. How do I get the XML data into the Crosshairs collection. I’ve currently got this in my toolTip constructor class:

    Crosshairs c = new Crosshairs();
    c.SetProperty("a",new {width="3", color="green"});
    c.SetProperty("b", new { width = "3", color = "blue" });
    crosshairs.Add(c);
    

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.

  1. I’ve added in the Converter class but the output I’m now getting is something like:

    tooltip: {
    borderColor: “#F00”
    crosshairs: {
    List: [
    {
    Value: {
    width: “3”
    color: “green”
    }
    Text: {
    width: “3”
    color: “blue”
    }
    }
    ]
    }
    enabled: true

    }

I did change the example converter to have the following rows:

foreach (Crosshairs item in listType) {
    //Add each entry to the dictionary.
    Dictionary<string, object> listDict = new Dictionary<string, object>();
    listDict.Add("Value", item.GetProperty("a"));
    listDict.Add("Text", item.GetProperty("b"));
    itemsList.Add(listDict);
}
result["List"] = itemsList;

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.

  1. 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.

If anyone could help with #1 above – importing the data from XML – to generic class rather than a predefined class it would really help.

Again – Many thanks for the help.
Alan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T14:21:22+00:00Added an answer on May 21, 2026 at 2:21 pm

    Define a simple Crosshairs type that is a wrapper for a string, object dictionary:

    class CrosshairsCollection : List<Crosshairs>
    {
    
    }
    
    class Crosshairs
    {
        private Dictionary<string, object> dict = new Dictionary<string,object>();
    
        public IEnumerable<KeyValuePair<string, object>> GetAllProperties()
        {
            foreach (string key in dict.Keys)
            {
                yield return new KeyValuePair<string, object>(key, dict[key]);
            }
        }
    
        public object GetProperty(string s)
        {
            object value;
    
            bool exists = dict.TryGetValue(s, out value);
            if (!exists)
            {
                value = null;
            }
    
            return value;
        }
    
        public void SetProperty(string s, object o)
        {
            if (!dict.ContainsKey(s))
            {
                dict.Add(s, o);
            }
            else
            {
                dict[s] = o;
            }
        }
    }
    

    Then implement a JavaScriptConverter for CrosshairsCollection, similar to this: http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptconverter.aspx

    Replace your List with CrosshairsCollection and register the JavaScriptConverter with your JavaScriptSerializer instance.

    [Edit: answers to follow up questions]

    I’m assuming that you have two follow-up questions, both numbered 1, and that the second one is “how do I get the JSON output to be more like my original example?” 🙂

    The way I interpreted your original XML was that a tooltip has multiple crosshairs, and not a single crosshairs with multiple, overlapping property definitions. The representation in the XML and your name usage in the code aren’t in accord though: you call the entire thing “crosshairs.” Bear that in mind as you’re reading my original and edited response.

    So to get an object that mimics the original XML, I would have expected you to provide the properties like so:

    CrosshairsCollection crosshairsList = new CrosshairsCollection();
    
    Crosshairs c1 = new Crosshairs();
    c1.SetProperty("width", 3);
    c1.SetProperty("color", "green");
    c1.SetProperty("padding", "5px");
    crosshairsList.Add(c1);
    
    Crosshairs c2 = new Crosshairs();
    c2.SetProperty("width", 3);
    c2.SetProperty("color", "blue");
    crosshairsList.Add(c2);
    

    Take that one step further, turn each Crosshairs initialization into a factory method, and plug it into your XML representation, like you’re doing in the HCToolTip constructor.

    Then, you can add each name value pair into the JSON output as they are:

    foreach (Crosshairs crosshairs in crosshairsList) {
        Dictionary<string, object> crosshairProps = new Dictionary<string, object>();
    
        foreach (KeyValuePair<string, object> prop in crosshairs.GetAllProperties()) {
            crosshairProps.Add(prop.Key, prop.Value);
        }
    
        itemsList.Add(crosshairProps);
    }
    
    result["crosshairs"] = itemsList;
    

    You might need to implement a JavaScriptConverter one level higher up, on ToolTip, in order to get an anonymous list as the property value for ToolTip.crosshairs.

    I hope what this is showing though is that really all you’re looking for is a key/value collection. I named the classes CrosshairsCollection and Crosshairs to try to draw the distinction between a list of <crosshairs> tags and the subtree of a <crosshairs> tag. But you could name them MultiPropertyItemCollection and MultiPropertyItem, or whatever, because there’s nothing type specific in the implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello i have the follow problem. I have e XML file and i know
I have the follow XML File: <Questionario> <Relacoes Marca=SADIA> <Questao> <IDEtapa> 1 </IDEtapa> <IDQuestao>
I have an XML file that starts like this: <Elements name=Entities xmlns=XS-GenerationToolElements> I'll have
I have an xml file providing data for a datagrid in Flex 2 that
I have an XML file, which I open in F# like this: let Bookmarks(xmlFile:string)
I have an xml file where I need to comment out a whole piece
I have an XML file loaded into a DOM document, I wish to iterate
I have an XML file and an XML schema in another file and I'd
I have a XML File like that <?xml version=1.0 encoding=utf-8 ?> <Configurations> <EmailConfiguration> <userName>xxxx</userName>
I have an xml file like this: <root> <item> <name>one</name> <status>good</status> </item> <item> <name>two</name>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.