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 1068629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:18:43+00:00 2026-05-16T20:18:43+00:00

i have a class like class CXmlData { String Data1=; String Data2=; List<String> lst=new

  • 0

i have a class like

class CXmlData
    {
        String Data1="";
        String Data2="";
        List<String> lst=new List<String>()
    }

and a Dictionary

Dictionary<String,CXmlData> dict=new Dictionary<String,CXmlData>();

The xml file is having the following structure

<Root>
    <ChildList>
        <Children>
            <Child Name="a1" val="A"/>
            <Child Name="a2" val="A"/>
            <Child Name="b1" val="B"/>
            <Child Name="c1" val="C"/>
            <Child Name="c2" val="C"/>
        </Childen>
        <Siblings_One>
              <Sibling Name="A" Xpos="0" Ypos=""/>
              <Sibling Name="B" Xpos="1" Ypos="1"/>
        </Sibling_One>
        <Siblings_Two>
              <Sibling Name="C" Xpos="0" Ypos="0"/>              
        </Sibling_Two>
    </ChildList>
</Root>

i need to read the data from the above xml file and to add to the dictionary

so the Resulting Dictionary will be like

    Key(String)          Value(Instance of CXmlData)

        "A"                   Data1="0"
                           Data2="2"
                              lst=new List<String>{"a1","a2"}

        "B"                   Data1="1" 
                              Data2="1"
                              lst=new List<String>{"b1"}

        "C"                   Data1="0"
                              Data2="2"
                              lst=new List<String>{"c1","c2"}

(if Xpos value is “0” then Data2(Ypos) value should be “2”)
Now I’m using like

1.Read Sibling_One child values

if Xpos value is “0” the take Ypos value as”2″

else keep the same

2.Read Sibling_Two child values

if Xpos value is “0” the take Ypos value as”2″

else keep the same

3.Read Children->Child values

Compare Sibling->Name attribute ==Child->Name

if matches then add the Child->Name attribute value to the list

i’m using Xml Namespace(System.Xml) and foreach loop for this, but its taking more time to complete the process

EDIT Sample Code

            XmlDocument XDoc = new XmlDocument();
            XDoc.Load(Application.StartupPath + "\\foo.xml");
            XmlElement XRoot = XDoc.DocumentElement;
            List<string> TempList = new List<string>();
            XmlNodeList XChildName = XDoc.SelectNodes("//ChildList/Sibling_One/Sibling");
            foreach (XmlNode ch in XChildName)
            {
                TempList .Add(ch.Attributes["Name"].Value);
                CXmlData P = new CXmlData();
                if (ch.Attributes["Xpos"].Value == "0")
                    P.Data2 = "2";
                else
                    P.Data2 = ch.Attributes["Ypos"].Value;
                P.Data1 = ch.Attributes["Xpos"].Value;
                dict.Add(ch.Attributes["NetName"].Value, P);

            }
            XChildName = XDoc.SelectNodes("//ChildList/Sibling_Two/Sibling");
            foreach (XmlNode ch in XChildName)
            {
                TempList .Add(ch.Attributes["Name"].Value);
                CXmlData P = new CXmlData();
                if (ch.Attributes["Xpos"].Value == "0")
                    P.Data2 = "2";
                else
                    P.Data2 = ch.Attributes["Ypos"].Value;
                P.Data1 = ch.Attributes["Xpos"].Value;
                dict.Add(ch.Attributes["Name"].Value, P);
            }

            foreach (string str in TempList )
            {
                List<String> lstChildValues = (from XmlNode xn in XDoc.SelectNodes("//ChildList/Children/Child")
                            where xn.Attributes["val"].Value == str
                            select xn.Attributes["Name"].Value).ToList();

                if (dict.Keys.Contains(str))
                {

                    CXmlData p = dict[str];
                    dict.Remove(str);
                    p.lst = lstChildValues;
                    dict.Add(str, p);
                }
            }

is there any way to do this using LINQ to Xml (System.xml.Linq)?

  • 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-16T20:18:44+00:00Added an answer on May 16, 2026 at 8:18 pm
     List<String>l_lstName = XDocument.Load("foo.xml")
                                   .Descendants("Sibling")
                                   .Select(Temp => Temp.Attribute("Name").Value).ToList();
    
    
            Dictionary<String, CXmlData> dict = new Dictionary<string, CXmlData>();
    
    
    
    
    
            dict = XDocument.Load("foo.xml")
                                   .Descendants("Sibling").ToDictionary(
                                   X => X.Attribute("Name").Value,
                                   X => new CXmlData
                                   {
                                       lst =XDocument.Load("foo.xml").Descendants("Children").Descendants("Child").Where(Temp=>Temp.Attribute("val").Value==X.Attribute("Name").Value).Select(Temp=>Temp.Attribute("Name").Value).ToList(),
                                       Data1 = X.Attribute("Xpos").Value == "0" ? "2" : X.Attribute("Ypos").Value,
                                       Data2 = X.Attribute("Xpos").Value
                                   }
                                   );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a class like: class myclass{ Long id; String a; Text b; }
I have a class like so: [JsonObject(MemberSerialization.OptIn)] public class foo { [JsonProperty(name_in_json)] public string
I have a class like this - class Messages { ... LinkedList<String> inputs; LinkedList<String>
I have a class like the one below: class Foo { private: std::map<std::string, Bar*>
I have a class like the following: class node { public: node* parent; std::list<node*>
I have a class like this: public class Contest { List<ContestTeam> Teams { get;
I have a class like this: public class BaseClass { public BaseClass(URL url, String
Hi have one class like this import java.util.ArrayList; public class MobilePhone { private String
I have a class like this : class User { String name; String password;
I have class like the following: class test { public string Name; public string

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.