1

How can I parse XML data into an array using C#?

<details> 
 <row> 
  <var name="Year" value="&quot;2008&quot;" /> 
  <var name="person" value="&quot;10202&quot;" /> 
 </row> 
 <row> 
  <var name="Year" value="&quot;2007&quot;" /> 
  <var name="person" value="&quot;11202&quot;" /> 
 </row> 
</details> 
6
  • 1
    I mean, give us Xml example, class description. More info. Commented Dec 19, 2010 at 18:40
  • Yes, but you should probably use a List or other typesafe collection instead of an Array. How to actually do it depends a bit on the structure of the data in the XML. Commented Dec 19, 2010 at 18:40
  • <details> <row> <var name="budgetYear" value="&quot;2008&quot;" /> <var name="account" value="&quot;10202&quot;" /> </row> <row> <var name="budgetYear" value="&quot;2007&quot;" /> <var name="account" value="&quot;11202&quot;" /> </row> Commented Dec 19, 2010 at 18:42
  • 1
    Add this xml to your question, please. Commented Dec 19, 2010 at 18:46
  • 1
    @Lambo - You should edit the question and add the XML to it. Commented Dec 19, 2010 at 18:48

3 Answers 3

4

From the example, it looks to me like you are hoping to get this:

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

This will create an IEnumerable<Dictionary<string, string>>, where each entry in the IEnumerable is a "row" and each entry in the inner dictionaries is a "variable".


If you are looking to create a more strongly-typed solution, and can guarantee the XML structure is always two variables, one named budgetYear and one named account, then something like this would work:

// Probably needs a better name.
struct YearAndAccount
{
    private readonly int budgetYear;
    private readonly string accountId;

    public int BudgetYear { get { return this.budgetYear; } }
    public string AccountId { get { return this.accountId; } }

    public YearAndAccount(int budgetYear, string accountId)
    {
        this.budgetYear = budgetYear;
        this.accountId = accountId;
    }
}

var rows = XDocument.Load("filename.xml")
                    .Root.Elements()
                    .Select(row => new YearAndAccount(
                        int.Parse(row.Elements().Single(el => el.Attribute("name").Value == "budgetYear")
                                                      .Attribute("value").Value),
                        row.Elements().Single(el => el.Attribute("name").Value == "account")
                                      .Attribute("value").Value));

This will create an IEnumerable<YearAndAccount>.


The parsing code for the strongly-typed solution is so amazingly icky because your XML is very poorly structured; a better structure would be something like

<details>
 <yearAndAccount>
   <budgetYear>2008</budgetYear>
   <account>10202</account>
 </yearAndAccount>
 <yearAndAccount>
   <budgetYear>2007</budgetYear>
   <account>11202</account>
 </yearAndAccount>
</details>

in which case the code would simply be

var rows = XDocument.Load("filename.xml")
                    .Root.Elements()
                    .Select(row => new YearAndAccount(row.Element("budgetYear").Value,
                                                      int.Parse(row.Element("account").Value)));
Sign up to request clarification or add additional context in comments.

Comments

2

Xml Serialization/Deserialization in .Net can be achieved by:

  1. XmlSerializer and Xml*Attributes in System.Xml.Serialization namespace
  2. DataContactSerializer and DataContract+DataMember attributes
  3. XmlReader and XmlWriter + Linq - processing raw xml-files.

Comments

0

What you want to achieve is called deserialization - there are many ways to achieve it.

In the .NET framework, one of the newer classes with good control over how this is achieved is the DataContractSerializer. You can see an example of how it works in the example at the bottom of the linked documentation.

Without concrete examples, this is as far as I can help.

3 Comments

the problem I want to do is to convert a csv file into a xml but it was overwriting the xml file already there so I was going to read in the data from the xml and add it to the generated data to amend the file. This way I can add in a checker for duplicate data and error checks.
@Lambo - You can deserialize the XML to a class using one of the serializer classes, then add the CSV data to your list and serialize it out to XML, after removing the duplicates.
I like that idea, will give it a try and get back to you all.

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.