0

I want to Convert My Data Table to XML. I Have two data Table Similar like this.

enter image description here

I want to convert similar like this.

<?xml version="1.0" encoding="utf-8"?>
<root>
  <member>
    <refid>1</refid>
    <fname>Indocin</fname>
    <lname>David</lname>
    <activities>
      <refid>1</refid>
      <act>Swimming</act>
    </activities>
  <activities>
      <refid>1</refid>
      <act>running</act>
    </activities>
    <Date>2013-09-17T18:10:01.3452408+05:30</Date>
  </member>
  <member>
    <refid>2</refid>
    <fname>Indocin</fname>
    <lname>David</lname>
    <activities>
      <refid>2</refid>
      <act>playing</act>
    </activities>
    <activities>
      <refid>2</refid>
      <act>running</act>
    </activities>
   <activities>
      <refid>2</refid>
      <act>swimming</act>
    </activities>
    <Date>2013-09-17T18:10:01.3452408+05:30</Date>
  </member>
</root>

I Have tried Below, but I need to do this with looping with datatable select statement for refid. can anyone please help me on this.

 XDocument doc = new XDocument(new XElement("root",
                                             new XElement("member",
                                                 new XElement("refid", 1),
                                                 new XElement("fname", "Indocin"),
                                                 new XElement("lname", "David"),
                                                 new XElement("activities", new XElement("refid",1),
                                                     new XElement("act","Swimming")),
                                                 new XElement("Date", DateTime.Now))));

2 Answers 2

1

I hope it will help you

private string CreateXML(DataTable table1, DataTable table2)
    {
        System.Text.StringBuilder sp = new System.Text.StringBuilder();

        sp.Append("<root>");
        for (int i = 0; i < table1.Rows.Count; i++)
        {

            sp.Append("<member>");
            sp.Append("<refid>" + table1.Rows[i]["refid"] + "</refid>");
            sp.Append("<fname>" + table1.Rows[i]["fname"] + "</fname>");
            sp.Append("<lname>" + table1.Rows[i]["lname"] + "</lname>");

            for (int j = 0; j < table2.Rows.Count; j++)
            {
                if (table1.Rows[i]["refid"] == table2.Rows[j]["refid"])
                {
                    sp.Append("<activities>");
                    sp.Append("<refid>" + table2.Rows[j]["refid"] + "</refid>");
                    sp.Append("<act>" + table2.Rows[j]["act"] + "</act>");

                    sp.Append("</activities>");
                }
            }

            sp.Append("<Date>" + table1.Rows[i]["Date"] + "</Date>");
            sp.Append("</member>");
        }

        sp.Append("</root>");
        return sp.ToString();
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the writeXML method to save it as XML Here is useful link http://msdn.microsoft.com/en-us/library/system.data.datatable.writexml.aspx

string result;
using (StringWriter sw = new StringWriter()) {
dataTable.WriteXml(sw);
result = sw.ToString();
}

Comments

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.