-2

Given the following Xml:

<Student number='2020'>
  <Subject>Comp</Subject>
  <Credintials>
    <Password>010101</Password>
    <PasswordLength>6</PasswordLength>
    <Contact>[email protected]</Contact>
  </Credintials>
  <PersonalDetails age='30' height='2'/>
  <Lecture age='30' height='2'>
    <StudentName>Hakeem</StudentName>
  </Lecture>
</Student>

I would want to print out the following list:

Student.@number=2020
Student.Subject=Comp
Student.Credintials.Password=010101
Student.Credintials.PasswordLength=6
[email protected]
Student.PersonalDetails.@age=30
Student.Lecture.@age=30
Student.PersonalDetails.@height=2
Student.Lecture.@height=2
Student.Lecture.StudentName=Hakeem

I am basically trying to get these paths for attributes and elements which have their values equal to the innerText, elements like StudentName, Password, Subject. atttributes like age, height etc

Thanks

6
  • Tried Xml Serialization ? Commented Nov 7, 2014 at 12:34
  • Have you tried anything yet? Have you read up on XML Serialization? Have you done a search on SO regarding how to create XML classes from a given XML? (Example answer) Commented Nov 7, 2014 at 12:34
  • I'd use LINQ to XML, personally... get all the elements using Descendants, and then write all the lines for that element. Commented Nov 7, 2014 at 12:36
  • Flattening hierarchy? Sounds easy: parse xml, go recursively through elements, generate output for each value and attribute. Commented Nov 7, 2014 at 12:36
  • 2
    P.S. 'Credentials' is misspelled throughout your code. :p Commented Nov 7, 2014 at 12:41

1 Answer 1

0

A method like this will print out what you expect

var xml  = @"<Student number='2020'>
                          <Subject>Comp</Subject>
                          <Credintials>
                            <Password>010101</Password>
                            <PasswordLength>6</PasswordLength>
                            <Contact>[email protected]</Contact>
                          </Credintials>
                          <PersonalDetails age='30' height='2'/>
                          <Lecture age='30' height='2'>
                            <StudentName>Hakeem</StudentName>
                          </Lecture>
                        </Student>";

var xmlParsed = XElement.Parse(xml);
GetNodeDescendantsAndPrint(xmlParsed);


public void GetNodeDescendantsAndPrint(XElement node, string nameToAppend= null)
{
    var name = string.IsNullOrEmpty(nameToAppend) 
                        ? node.Name.LocalName  
                        : nameToAppend;
    foreach (var att in node.Attributes())
    {
        Console.WriteLine(name + ".@" + att.Name.LocalName + "=" + att.Value);
    }
    var descendants = node.Elements();
    if (descendants.Any())
    {
        foreach (var innerNode in descendants.OfType<XElement>())
        {
            GetNodeDescendantsAndPrint(innerNode, 
                                        name+"." + innerNode.Name.LocalName );
        }
    }
    else
    {
            Console.WriteLine(name + "=" + node.Value);
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

Just some little bugs which i think i can sort out, but great idea , i liked it
yeah I might be printing out in one or 2 scenarios where I shouldn't, but at least you have an idea on how to continue. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.