-2

Given the following Xml:

<Student number='2020'>
  <Subject>Comp</Subject>
  <Credintials>
    <Password>010101</Password>
    <PasswordLength>6</PasswordLength>
    <Contact>contact@example.com</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
Student.Credintials.Contact=contact@example.com
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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Tee
  • 39
  • 1
  • 9
  • Tried Xml Serialization ? – SanyTiger Nov 07 '14 at 12:34
  • Have you tried anything yet? Have you read up on [XML Serialization](http://msdn.microsoft.com/en-us/library/182eeyhh%28v=vs.110%29.aspx)? Have you done a search on SO regarding how to create XML classes from a given XML? ([Example answer](http://stackoverflow.com/a/26633029/3864353)) – Bernd Linde Nov 07 '14 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. – Jon Skeet Nov 07 '14 at 12:36
  • Flattening hierarchy? Sounds easy: parse xml, go recursively through elements, generate output for each value and attribute. – Sinatr Nov 07 '14 at 12:36
  • 2
    P.S. 'Credentials' is misspelled throughout your code. :p – Rufus L Nov 07 '14 at 12:41
  • I would use serialization personally. Create an object that represents what the XML should populate. Serialize it to that object. I have a very small example on the following post: http://stackoverflow.com/questions/26789111/how-to-create-and-export-data-to-an-xml-file – James Shaw Nov 07 '14 at 12:54

1 Answers1

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>contact@example.com</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);
    }

}
TYY
  • 2,702
  • 1
  • 13
  • 14
  • Just some little bugs which i think i can sort out, but great idea , i liked it – Tee Nov 07 '14 at 14:08
  • 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. :) – TYY Nov 07 '14 at 15:19