Friday 11 January 2013

Read values from XML file using c# .net in SharePoint 2010

I had a requirement to get values from the XML file & perform further operation.
The following code shows how to read a XML file & populate the data from XML to data table & perform further operations.
Add the xml file to the SharePoint solution in C#.Net. Right Click Project – Select Add New Item – Select XML File.
Give the Proper name to the XML File & add the below xml code in the XML file.
<Items>

  <Field Url="http://site-name" Source="List-Library-name" Destination="List-Library-name" Type="Library"></Field>

  <Field Url="http://site-name" Source="List-Library-name" Destination="List-Library-name" Type="List"></Field>

</Items>


Now after adding the XML file to fetch the data from the XML file add the path of the XML file in the app.config to let the user access the XML file. Add the below key & value in the appSettings tag of the app.congif file.

<appSettings>

    <add key="RecordListXML" value="SiteNListDetails.xml" />

</appSettings>


private static string SiteNListDetails = ConfigurationManager.AppSettings["RecordListXML"];



///Method to get Data from XML.
public void GetDetails()
{
  DataTable dt;
  string url = string.Empty;
  string source = string.Empty;
  string destination = string.Empty;
  string type = string.Empty;
  try
  {
    ///Call Method to get the data from XML
     dt = ReadXML();
     ///Start of For Loop to get parameter to perform operation.
     for (int i = 0; i < dt.Rows.Count; i++)
    {
       ///Perform Operation.
    }
    ///End of for Loop.
   }
  catch (Exception ex)
  {
   }
}
 /// ReadXMl is a method to read values from XML File & Add values in DataTable
///Returns DataTable with values
 public DataTable ReadXML()
{
  string url = string.Empty;
  string source = string.Empty;
  string destination = string.Empty;
  string type = string.Empty;
  DataRow row;
  DataTable XMLTable= CreateXMLTableStructure();
   string xmlPath = Path.GetFullPath(Environment.CurrentDirectory) + "\\" + SiteNListDetails;
  XDocument groupsXml = XDocument.Load(xmlPath);
  try
  {
    var fields = from fieldsGroup in groupsXml.Root.Elements("Field")
    select new
    {
      url = fieldsGroup.Attribute("Url").Value,
      source = fieldsGroup.Attribute("Source").Value,
      destination = fieldsGroup.Attribute("Destination").Value,
      type = fieldsGroup.Attribute("Type").Value
    };
     ///Start of For Loop: Add row with values from xml to DataTable
     foreach (var fieldsGroup in fields)
    {
       row = XMLTable.NewRow();
      row["Url"] = fieldsGroup.url;
      row["Source"] = fieldsGroup.source;
      row["Destination"] = fieldsGroup.destination;
      row["Type"] = fieldsGroup.type;
      XMLTable.Rows.Add(row);
     }
    ///End of For Loop
  }
  catch (Exception ex)
  {
  }
  return XMLTable;
}
/// CreateXMLTableStructure is  a method to Create DataTable Structure.
///Returns DataTable Structure
public DataTable CreateXMLTableStructure()
{
  DataTable XMLTable = new DataTable();
  DataColumn url = new DataColumn("Url");
  DataColumn source = new DataColumn("Source");
  DataColumn destination = new DataColumn("Destination");
  DataColumn type = new DataColumn("Type");
  try
  {
    /// Add Columns to DataTable.
    url.DataType = System.Type.GetType("System.String");
    source.DataType = System.Type.GetType("System.String");
    destination.DataType = System.Type.GetType("System.String");
    type.DataType = System.Type.GetType("System.String");
    XMLTable.Columns.Add(url);
    XMLTable.Columns.Add(source);
    XMLTable.Columns.Add(destination);
    XMLTable.Columns.Add(type);
   }
  catch (Exception ex)
  {
   }
  return XMLTable;
}

No comments:

Post a Comment