0

I have this xelement:

<adress name="company1" street="street1" City="city1" <branch key="1" value="branch1" /><language key="1" value="langauge1" /> </adress>

as you see there are several attributes in the element adress and it inherits the elements branch and language which also containing some attributes.

I want a table like this:

name street city branchvalue branchkey languagevalue languagekey

How can I do this? I can hardcode it, because the xml scheme is everytime the same but, i would like to have a dynamic and slim solution

steve
  • 123
  • 1
  • 14
  • Have you tried anything? And also why you have C# and vb.net in your tags. – शेखर Aug 21 '15 at 11:47
  • At the moment I create a table, hardcode the columns and put every element.attribute into his column. Maybe my question is vague. I would like to know if there already exists a way in the framework to do what I want. – steve Aug 21 '15 at 11:51
  • else I need to make the effort and write my own method. – steve Aug 21 '15 at 11:51

1 Answers1

0

Here's an example of one way to do it. You really should do null checking and not assume all attributes are present with values, but if you can trust it, here you go:

    string strElem = "<adress name=\"company1\" street=\"street1\" City=\"city1\"><branch key=\"1\" value=\"branch1\" /><language key=\"1\" value=\"langauge1\" /> </adress>";
    XElement xel = XElement.Parse(strElem);

    var branch = xel.Element("branch");
    var lang = xel.Element("language");

    var theTable = new {
        name = xel.Attribute("name").Value, 
        street = xel.Attribute("street").Value, 
        city = xel.Attribute("City").Value,
        branchkey = branch.Attribute("key").Value,
        branchvalue = branch.Attribute("value").Value,
        languagevalue = lang.Attribute("key").Value,
        languagekey = lang.Attribute("value").Value
        };

Update

In light of new evidence... I'm still not sure if this suits your needs, but you can dynamically create a Dictionary. From there you can do whatever you want with the data:

            string strElem = "<adress name=\"company1\" street=\"street1\" City=\"city1\"><branch key=\"1\" value=\"branch1\" /><language key=\"1\" value=\"langauge1\" /> </adress>";
            XElement xel = XElement.Parse(strElem);

            var theTable = new ExpandoObject() as IDictionary<string, Object>;
            foreach (XAttribute attr in xel.Attributes())
            {
                theTable.Add(attr.Name.LocalName, attr.Value);
            }

            foreach (XElement el in xel.Elements())
            {
                foreach (XAttribute attr in el.Attributes())
                {
                    theTable.Add(el.Name + attr.Name.LocalName, attr.Value);
                }
            }

enter image description here

Crowcoder
  • 11,250
  • 3
  • 36
  • 45
  • this is what I undestood as hardcoded and what I already have. I want a dynamic approach, where the attributes names can change. – steve Aug 21 '15 at 12:33