Conversion Between XML and datatable

Source: Internet
Author: User

Public static datatable getdatatable (string xmlstr) <br/>{< br/> xmldocument Doc = new xmldocument (); <br/> Doc. loadxml (xmlstr); </P> <p> xmlnodelist xlist = Doc. selectnodes ("// able/rows"); <br/> datatable dt = new datatable (); <br/> datarow Dr; </P> <p> for (INT I = 0; I <xlist. count; I ++) <br/>{< br/> DR = DT. newrow (); <br/> xmlelement Xe = (xmlelement) xlist. item (I); <br/> for (Int J = 0; j <Xe. Attributes. Count; j ++) <br/>{< br/> If (! DT. columns. contains ("@" + Xe. attributes [J]. name) <br/> DT. columns. add ("@" + Xe. attributes [J]. name); <br/> Dr ["@" + Xe. attributes [J]. name] = Xe. attributes [J]. value; <br/>}< br/> for (Int J = 0; j <Xe. childnodes. count; j ++) <br/>{< br/> If (! DT. columns. contains (Xe. childnodes. item (j ). name) <br/> DT. columns. add (Xe. childnodes. item (j ). name); <br/> Dr [Xe. childnodes. item (j ). name] = Xe. childnodes. item (j ). innertext; <br/>}< br/> DT. rows. add (DR); <br/>}< br/> return DT; <br/>}</P> <p> Public static string getxml (datatable DT) <br/>{< br/> string strxml = @ "<? XML version = '1. 0' encoding = 'utf-8'?> <Datatable/> "; <br/> xmldocument Doc = new xmldocument (); <br/> Doc. loadxml (strxml); <br/> xmlnode root = Doc. selectsinglenode ("// datatable"); <br/> // create a subnode <br/> for (Int J = 0; j <DT. rows. count; j ++) <br/>{< br/> xmlelement Xe = Doc. createelement ("rows"); <br/> xmlelement xechild = NULL; <br/> If (! Object. equals (DT, null) <br/>{< br/> for (INT I = 0; I <DT. columns. count; I ++) <br/>{< br/> If (DT. columns [I]. columnname. startswith ("@") <br/>{< br/> string attributename = DT. columns [I]. columnname. replace ("@", ""); <br/> // set attributes for this subnode <br/> Xe. setattribute (attributename, DT. rows [J] [I]. tostring (); <br/>}< br/> else <br/>{< br/> xechild = Doc. createelement (DT. columns [I]. columnname); </P> <p> try <br/>{< br/> xechild. innerxml = DT. rows [J] [I]. tostring (); <br/>}< br/> catch <br/> {<br/> xechild. innertext = DT. rows [J] [I]. tostring (); <br/>}< br/> Xe. appendchild (xechild); <br/>}< br/> // Save the sub-node Settings <br/> root. appendchild (xe); <br/>}< br/> return Doc. innerxml. tostring (); <br/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.