locked
Reading an XML file RRS feed

  • Question

  • User351619809 posted

    Hi All,

    This is the first time, I need to parse an XML file.  The structure is like this:

    <?xml version="1.0" encoding="UTF-8"?>
    
    
    <P>Test1</P>
    <p>Test2</P>
    <p>This is a Test line1</P>
    <table>
    <TR>
    <TH>First Component</TH>
    <TH>Second Component</TH>
    </TR>
    <TR>
    <TH>NCOL</TH>
    <TD>Col2</TD>
    <TD>Col3</TD>
    <TD>Col4</TD>
    <TD>Col5</TD>
    <TD>Col6</TD>
    <TD>Col7</TD>
    </TR>
    <TR>
      <TH>3245</TH>
    <TD>Testing</TD>
    <TD/>
    <TD>45678-9876</TD>
    <TD/>
    <TD/>
    <TD/>
    <TR>
       <TH>567P</TH>
      <TD>Testing2</TD>
    <TD>Test45</TD>
    <TD/>
    <TD/>
    <TD/>
    
    </TR>
    .
    .
    .
    .
    .
    </TABLE>
    <P>1. This is a test</p>
    <P>2. This is second line></p>
    <p>3. This is the third line</P>
    .
    .
    .
    

    I need to parse this file and put the contents in the database. First I have to parse <P> and then all the columns and then <P> again. How can I achieve that.

    Monday, September 15, 2014 1:56 PM

Answers

  • User1918509225 posted

    Hi anjaliagarwal15,

    For your issue,i created a demo for you.

    First ,I image you have a xml  named "test.xml" like below:

    <?xml version="1.0" encoding="utf-8" ?>
    <Main>
    <TR>
    <TH>NCOL</TH>
    <TD>Col2</TD>
    <TD>Col3</TD>
    <TD>Col4</TD>
    <TD>Col5</TD>
    <TD>Col6</TD>
    <TD>Col7</TD>
    </TR>
    <TR>
    <TH>3245</TH>
    <TD>Testing</TD>
    <TD/>
    <TD>45678-9876</TD>
    <TD/>
    <TD/>
    <TD/>
    </TR>
    </Main>

    Second ,you can try the code to get the values in pairs, store it in the dictionary:

     XElement ex = XElement.Load(Server.MapPath("test.xml"));
    IEnumerable<XElement> q = from ele in ex.Elements("TR") select ele; XElement list1 = q.ElementAt(0); XElement list2 = q.ElementAt(1);
    int num = list1.Elements().Count(); Dictionary<string, string> dic = new Dictionary<string, string>(); for (int i = 0; i < num; i++) { string val1 = list1.Elements().ElementAt(i).Value; string val2 = list2.Elements().ElementAt(i).Value; dic[val1] = val2; }

    Best Regards,

    Kevin Shen.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 15, 2014 11:21 PM

All replies

  • User1508394307 posted

    I belive this is not xml (or at least it is not valid). 

    See MSDN for examples: http://msdn.microsoft.com/en-us/library/cc189056(VS.95).aspx 

    Monday, September 15, 2014 2:12 PM
  • User351619809 posted

    This link solved almost everything, but just wondering if it is possible to get the values in pairs so I have

    <TR>
    <TH>NCOL</TH>
    <TD>Col2</TD>
    <TD>Col3</TD>
    <TD>Col4</TD>
    <TD>Col5</TD>
    <TD>Col6</TD>
    <TD>Col7</TD>
    </TR>
    <TR>
      <TH>3245</TH>
    <TD>Testing</TD>
    <TD/>
    <TD>45678-9876</TD>
    <TD/>
    <TD/>
    <TD/>
    <TR>
       <TH>567P</TH>
      <TD>Testing2</TD>
    <TD>Test45</TD>
    <TD/>
    <TD/>
    <TD/>
    
    </TR>

    so can I have NCOL has a value of 3245 , col2 has a value of Testing , Col3 is empty.

    Monday, September 15, 2014 3:26 PM
  • User1918509225 posted

    Hi anjaliagarwal15,

    For your issue,i created a demo for you.

    First ,I image you have a xml  named "test.xml" like below:

    <?xml version="1.0" encoding="utf-8" ?>
    <Main>
    <TR>
    <TH>NCOL</TH>
    <TD>Col2</TD>
    <TD>Col3</TD>
    <TD>Col4</TD>
    <TD>Col5</TD>
    <TD>Col6</TD>
    <TD>Col7</TD>
    </TR>
    <TR>
    <TH>3245</TH>
    <TD>Testing</TD>
    <TD/>
    <TD>45678-9876</TD>
    <TD/>
    <TD/>
    <TD/>
    </TR>
    </Main>

    Second ,you can try the code to get the values in pairs, store it in the dictionary:

     XElement ex = XElement.Load(Server.MapPath("test.xml"));
    IEnumerable<XElement> q = from ele in ex.Elements("TR") select ele; XElement list1 = q.ElementAt(0); XElement list2 = q.ElementAt(1);
    int num = list1.Elements().Count(); Dictionary<string, string> dic = new Dictionary<string, string>(); for (int i = 0; i < num; i++) { string val1 = list1.Elements().ElementAt(i).Value; string val2 = list2.Elements().ElementAt(i).Value; dic[val1] = val2; }

    Best Regards,

    Kevin Shen.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 15, 2014 11:21 PM