1

Currently the application that I am working on returns XML into a string.

String xml = getData();

But now I need to convert this to a string array, the Xml will something like this:

<details> 
 <row> 
  <Year>2014</Year> 
  <Person>Bob</Person> 
 </row> 
 <row> 
  <Year>2013</Year> 
  <Person>Fred</Person> 
 </row> 
</details> 

Ive seen how to do this if the XML is saved in a file but this wont be an option. Any Ideas?

I would like to have something like a dictionary array or list of arrays with the values looking like this: year, year, year

person, person, person

4
  • 2
    What exactly do you want to have in the array? Each line of the XML? An array with names of the persons? Or anything else? Commented Apr 19, 2014 at 8:57
  • Share a format of the array you are expecting from the XML Commented Apr 19, 2014 at 9:01
  • Edited to what Im trying to get out of it Commented Apr 19, 2014 at 9:11
  • Are you sure your xml has a valid structure? You are declaring elements with 2 attributes (name and value) but without a name Commented Apr 19, 2014 at 9:28

3 Answers 3

4

You can try this way :

var doc = XDocument.Parse(xml);
var rows = from row in doc.Root.Elements("row") select row;

//array of person
var persons = rows.Select(o => (string) o.Element("Person")).ToArray();
//array of year
var years = rows.Select(o => (string)o.Element("Year")).ToArray();

If you meant to get all element inside <row> in single array, try this way :

//you can remove OrderBy part if it isn't necessary
var result = rows.SelectMany(o => o.Elements())
                 .OrderBy(o => o.Name.LocalName)
                 .Select(o => (string) o).ToArray();
Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way of doing this without knowing the name "row" or the xml structure at compile time?
Yes, I'm sure there is a way. But that depends on possible XML structures you can have. Try to post several sample XMLs, so that we can't hardcode element name "row"
Well assuming it has a similar structure but perhaps different element names?
okay, if that is the case, simply replace Elements("row") with Elements()
0

As per answer given in this post

var rows = XDocument.Load("filename.xml")
                    .Root.Elements()
                    .Select(row => row.Elements()
                                      .ToDictionary(v => v.Attribute("name").Value,
                                                    v => v.Attribute("value").Value);

4 Comments

Can I use the string datatype with the xml in as the parameter for the load? otherwise this wont help
yes you can pass string in load method see the defination public static XDocument Load(TextReader textReader);
Can this method be made dynamic, for example if the amount of attributes and their names are not known at runtime
It can be made but you have to pass attribute name also dynamically i.e. instead of hardcoding it pass it dynamically
0

Assuming that "details" is the root element of your XmlFile and the name of the file is "XMLFile1.xml", you can use this:

XmlDocument doc = new XmlDocument();
doc.Load("XMLFile1.xml");

var rows = doc.GetElementsByTagName("row").Cast<XmlElement>();
var persons = rows.SelectMany(x => x.GetElementsByTagName("Person")
    .Cast<XmlElement>()).ToArray().Select(x => x.InnerText).ToArray();
var years = rows.SelectMany(x => x.GetElementsByTagName("Year")
    .Cast<XmlElement>()).ToArray().Select(x => x.InnerText).ToArray();

And don't forget to include the Linq-namespace.

1 Comment

Is there a way of doing this without knowing the name "row" or the xml structure at compile time?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.