Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,


I have an XMLFile in my solution called XMLFile.xml. It contains following data.
XML
<Report>
    <ReportName>
      Daily
    </ReportName>
    <StoredProcName>sp_GetDatafromTB</StoredProcName>
  </Report>
  <Report>
    <ReportName>
      Weekly
    </ReportName>
    <StoredProcName>sp_GetDatafromTable</StoredProcName>
  </Report>
  <Report>
    <ReportName>
      Monthly
    </ReportName>
    <StoredProcName>sp_GetDatafromTB</StoredProcName>
  </Report>
  <Report>
    <ReportName>
      Yearly
    </ReportName>
    <StoredProcName>sp_GetDatafromTable</StoredProcName>
  </Report>




Now on button click event i need to parse the xml file and and get the name of the StoredProcName when the name of the ReportName is equal to the name of the item selected in the dropdownlist (in UI) and bind it to a gridview. I am able to parse XML file but i how do i get name of the child node which can be used to run stored procedure. Please help me.
Posted

1 solution

You'll need to start by fixing the XML document - it currently has multiple root nodes, which is not allowed.

To parse it, try Linq to XML:
https://msdn.microsoft.com/en-us/library/bb387098.aspx[^]
XLINQ Introduction Part 3 Of 3[^]
C#
XDocument document = XDocument.Load("path\to\your\file.xml");

IEnumerable<xelement> query = 
    from XElement el in doc.Descendants("Report")
    let name = ((string)el.Element("ReportName") ?? string.Empty).Trim()
    where string.Equals(name, theReportNameToFind, StringComparison.Ordinal)
    select el;

XElement reportNode = query.First();
// This throws an exception if there are no matching reports.
// Alternatively, use "FirstOrDefault" and check for a null return value.

string procedureName = (string)reportNode.Element("StoredProcName");
</xelement>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900