Click here to Skip to main content
16,016,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all!

I have an XML and I read and store data from there.
like:
<root>
  <main>
    <1>sample</1>
    <2>sample</2>
    <context>sample</context>
    <context>sample</context>
    <context>sample</context>
    <context>sample</context>
  </main>
<main>
    <1>sample</1>
    <2>sample</2>
    <context>sample</context>
    <context>sample</context>
    <context>sample</context>
  </main>
<main>
    <1>sample</1>
    <2>sample</2>
  </main>
</root>


For some reason with the code below I can't store a list of context per class.

What I have tried:

I'm saving all the "main" to a new class like this:
ht.Add(new HandledTUs { SOURCE = 1, TARGET = 2, context = context,});


Class looks like this:

public class TUs
        {
            private List<string> _CONTEXT = new List<string>();
            

            public string 1{ get; set; }
            public string 2{ get; set; }
            public List<string> CONTEXT { get { return _CONTEXT; } set { _CONTEXT = value; } }

            

        }
Posted
Updated 3-Dec-18 2:11am
Comments
Richard MacCutchan 3-Dec-18 4:41am    
None of that code makes sense. What is ht, handledTUs, TUs etc.? Where are those objects created, how is the list populated ... ?

1 solution

To add to what Richard says, 1 and 2 are not value variable or property names, which cannot start with a numeric character.
So your C# code is bad - it won't compile - and the same applies to XML field names which must also start with a letter of underscore.

So your code is bad, yoru data is bad - I'm not surprised that whatever code you are using won't work!
If I correct your xml:
XML
<?xml version="1.0" encoding="utf-8" ?>
<root>
  <main>
    <A1>sample</A1>
    <A2>sample</A2>
    <context>sample</context>
    <context>sample</context>
    <context>sample</context>
    <context>sample</context>
  </main>
<main>
    <A1>sample</A1>
    <A2>sample</A2>
    <context>sample</context>
    <context>sample</context>
    <context>sample</context>
  </main>
<main>
    <A1>sample</A1>
    <A2>sample</A2>
  </main>
</root>
And use VS to generate the classes:

C#
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class root
    {

    private rootMain[] mainField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("main")]
    public rootMain[] main
        {
        get
            {
            return this.mainField;
            }
        set
            {
            this.mainField = value;
            }
        }
    }

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class rootMain
    {

    private string a1Field;

    private string a2Field;

    private string[] contextField;

    /// <remarks/>
    public string A1
        {
        get
            {
            return this.a1Field;
            }
        set
            {
            this.a1Field = value;
            }
        }

    /// <remarks/>
    public string A2
        {
        get
            {
            return this.a2Field;
            }
        set
            {
            this.a2Field = value;
            }
        }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("context")]
    public string[] context
        {
        get
            {
            return this.contextField;
            }
        set
            {
            this.contextField = value;
            }
        }
    }
Then the rootMain class looks like your TUs class.
(VS can generate XML classes for you from XML data - create a new empty class file, and select "Edit...Paste Special...Paste XML as classes" from the menu bar and it will generate classes based on the clipboard data.)
 
Share this answer
 
Comments
Richard MacCutchan 3-Dec-18 8:37am    
Useful tip, something I was not aware of.
Member 13050667 3-Dec-18 11:18am    
Sorry, Please disreguard from the naming, I was in rush. That is just a short easy sample. Of course the real xml has the proper node names. I just wanted to show a sample. Also the NDA not allows me to show the original code. But thank you for the help! It gave me some useful ideas!
OriginalGriff 3-Dec-18 11:28am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900