Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 800499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:18:00+00:00 2026-05-14T23:18:00+00:00

I have an XML document that has a collection of objects. Each object has

  • 0

I have an XML document that has a collection of objects. Each object has a key/value pair of label and value. I am trying to convert this into a DataSet, but when I do ds.ReadXml(xmlFile), then it creates two columns: label and value.

What I would like is to have a column for each “label” and the value to be part of the row. here is my sample of the XML:

<responses>
  <response>
    <properties id="1" Form="Account Request" Date="Tuesday, March 16, 2010 5:04:26 PM" Confirmation="True" />
    <fields>
      <field>
        <label>Name</label>
        <value>John</value>
      </field>
      <field>
        <label>Email</label>
        <value>John@Doe.com</value>
      </field>
      <field>
        <label>Website</label>
        <value>http://domain1.com</value>
      </field>
      <field>
        <label>Phone</label>
        <value>999-999-9999</value>
      </field>
      <field>
        <label>Place of Birth</label>
        <value>Earth</value>
      </field>
      <field>
        <label>Misc</label>
        <value>Misc</value>
      </field>
      <field>
        <label>Comments</label>
        <value />
      </field>
      <field>
        <label>Agree to Terms?</label>
        <value>True</value>
      </field>
    </fields>
  </response>
  <response>
    <properties id="2" Form="Account Request" Date="Tuesday, March 17, 2010 5:04:26 PM" Confirmation="True" />
    <fields>
      <field>
        <label>Name</label>
        <value>John2</value>
      </field>
      <field>
        <label>Email</label>
        <value>John2@Doe.com</value>
      </field>
      <field>
        <label>Website</label>
        <value>http://domain2.com</value>
      </field>
      <field>
        <label>Phone</label>
        <value>999-999-9999</value>
      </field>
      <field>
        <label>Place of Birth</label>
        <value>Earth</value>
      </field>
      <field>
        <label>Misc</label>
        <value>Misc</value>
      </field>
      <field>
        <label>Comments</label>
        <value />
      </field>
      <field>
        <label>Agree to Terms?</label>
        <value>True</value>
      </field>
    </fields>
  </response>
  <response>
    <properties id="3" Form="Account Request" Date="Tuesday, March 18, 2010 5:04:26 PM" Confirmation="True" />
    <fields>
      <field>
        <label>Name</label>
        <value>John3</value>
      </field>
      <field>
        <label>Email</label>
        <value>John3@Doe.com</value>
      </field>
      <field>
        <label>Website</label>
        <value>http://domain3.com</value>
      </field>
      <field>
        <label>Phone</label>
        <value>999-999-9999</value>
      </field>
      <field>
        <label>Place of Birth</label>
        <value>Earth</value>
      </field>
      <field>
        <label>Misc</label>
        <value>Misc</value>
      </field>
      <field>
        <label>Comments</label>
        <value />
      </field>
      <field>
        <label>Agree to Terms?</label>
        <value>True</value>
      </field>
    </fields>
  </response>
  <response>
    <properties id="4" Form="Account Request" Date="Tuesday, March 19, 2010 5:04:26 PM" Confirmation="True" />
    <fields>
      <field>
        <label>Name</label>
        <value>John</value>
      </field>
      <field>
        <label>Email</label>
        <value>John4@Doe.com</value>
      </field>
      <field>
        <label>Website</label>
        <value>http://domain4.com</value>
      </field>
      <field>
        <label>Phone</label>
        <value>999-999-9999</value>
      </field>
      <field>
        <label>Place of Birth</label>
        <value>Earth</value>
      </field>
      <field>
        <label>Misc</label>
        <value>Misc</value>
      </field>
      <field>
        <label>Comments</label>
        <value />
      </field>
      <field>
        <label>Agree to Terms?</label>
        <value>True</value>
      </field>
    </fields>
  </response>
</responses>

How would I convert this to a DataSet so that I can load it into a gridview with the columns: Name, Email, Website, Phone, Place of Birth, Misc, Comments, and Agree to Terms?

Then row 1 would be:
John, John@Doe.com, http://domain1.com, 999-999-9999, Earth, Misc, , True

How can I do this with the XML provided?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:18:00+00:00Added an answer on May 14, 2026 at 11:18 pm

    You’re going to have to transform your data in order to use it the way you want. As you’ve seen, you have a bad structure.

    I suggest that you create an empty dataset in Visual Studio (from Add->New Item), then set it to look the way you’d like it to look. Write some code to add a little test data, then write it to a file using DataSet.WriteXml. That will show you what your proposed structure would look like.

    I then recommend that you use LINQ to XML to transform your input XML into the new format.


    Here’s an example of using LINQ to XML to transform your data:

    public static void TransformIt(TextWriter output)
    {
        var inputDocument = XDocument.Parse(INPUT_XML);
        if (inputDocument.Root == null)
        {
            return;
        }
    
        var doc = new XDocument(
            new XElement(
                "responses",
                from response in inputDocument.Root.Elements()
                select new XElement(
                    "response",
                    from lv in GetResponseLabels(response)
                    select MakeResponse(lv.Label, lv.Value))));
    
        var settings = new XmlWriterSettings
        {
            Encoding = Encoding.UTF8,
            Indent = true,
        };
        using (var writer = XmlWriter.Create(output, settings))
        {
            if (writer == null)
            {
                return;
            }
    
            doc.WriteTo(writer);
        }
    }
    
    private static XElement MakeResponse(string label, string value)
    {
        var trimmedLabel = label.Replace(" ", String.Empty).Replace("?", String.Empty);
        return new XElement(trimmedLabel, value);
    }
    
    private static IEnumerable<LabelAndValue> GetResponseLabels(XContainer response)
    {
        var fieldsElement = response.Element("fields");
        if (fieldsElement == null)
        {
            return null;
        }
    
        return from field in fieldsElement.Elements("field")
               let valueElement = field.Element("value")
               let labelElement = field.Element("label")
               select new LabelAndValue
               {
                   Label = labelElement == null ? "Unknown" : labelElement.Value,
                   Value = valueElement == null ? null : valueElement.Value
               };
    }
    
    private struct LabelAndValue
    {
        public string Label { get; set; }
        public string Value { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML document that has several <item> elements. Inside each, there might
I am trying to parse an xml document that I have created. However xml.Descendants(value)
I have XML tag that has the content which is HTML document. <xml-tag> <!--
I have an XML document that I'm trying to search through. The Main structure
I have an XML document that I generate from an Entity Framework object. The
I have an XML document that has dates in standard ISO 8601 format. Like
I have an XML document that has an encoded non-XML PDF contained with in
I'm parsing an XML document that has nodes like the following: <objects> <dog> <data1>...</data1>
I have an XML document that has a TextBlock that contains HTML code. <TextBlock>
I have a xml document that has a record set like this. <document> <row>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.