Friday, April 17, 2009

Load an Xml File into a Class Collection using LINQ


Work a lot with Xml using the C# language? You will know that to load an Xml
file it can be quite extensive at times depending on how deep your requirements
send you. I personally prefer to work with classes that are populated with the
data from the Xml and bind my controls to such. I have put together a simple
example of loading data from an xml and building a collection of a given class.
Then binding a combobox to this list.


Here is my Xml file:
xmlversion="1.0"encoding="utf-8" ?>
<StageTypes>
<StageTypename="SUB">
<StageItemvalue="CP">Customer PlanStageItem>
<StageItemvalue="FF"> ContactStageItem>
<StageItemvalue="OC">Other ContactStageItem>
<StageItemvalue="MD">Medical/DentalStageItem>
<StageItemvalue="LG">LegalStageItem>
<StageItemvalue="OT">OtherStageItem>
StageType>
<StageTypename="FSU">
<StageItemvalue="FP">Family PlanStageItem>
<StageItemvalue="FF"> ContactStageItem>
<StageItemvalue="OC">Other ContactStageItem>
<StageItemvalue="MD">Medical/DentalStageItem>
<StageItemvalue="LG">LegalStageItem>
<StageItemvalue="OT">OtherStageItem>
StageType>
StageTypes>


Here is my StageTypeItem Class I will use to store the Text and Value for a combobox:
public class StageTypeItem
{
///
/// Initializes a new instance of the StageTypeItem class.
///
public StageTypeItem()
{
}
private string dataValue;
public string DataValue
{
get { return dataValue; }
set
{
dataValue = value;
}
}
private string dataText;
public string DataText
{
get { return dataText; }
set
{
dataText = value;
}
}
}


Here is the code to build the list:
public static List<StageTypeItem> LoadStageType(string stageType)
{
XDocument doc = XDocument.Parse(Properties.Resources.StageTypes, LoadOptions.None);
var stageTypes = from st in doc.Descendants("StageType").Descendants("StageItem")
where st.Parent.Attribute("name").Value == stageType
select new StageTypeItem
{
DataText = st.Value,
DataValue = st.Attribute("value").Value
};
return stageTypes.ToList<StageTypeItem>();
}


So I have a Root of StageType, with children StageItem in the Xml. I want to load the values where StageType name = passed parameter stageType. So assume I pass “SUB” in Xml I want the SUB children (StageItem only) to be returned:
<StageTypename="SUB">
<StageItemvalue="CP">Customer PlanStageItem>
<StageItemvalue="FF">ContactStageItem>
<StageItemvalue="OC">Other ContactStageItem>
<StageItemvalue="MD">Medical/DentalStageItem>
<StageItemvalue="LG">LegalStageItem>
<StageItemvalue="OT">OtherStageItem>
StageType>


Here I am saying load all Descendants of StageItem:
var stageTypes = from st in doc.Descendants("StageType").Descendants("StageItem")


But then I say, only StageItem where its parent has the name “SUB”, remember stageType is the parameter and I passed “SUB”
where st.Parent.Attribute("name").Value == stageType


This part creates a class of type StageTypeItem and I populate it from the Xml values:
select new StageTypeItem
{
DataText = st.Value, // the value of the Element, example first one is “Customer Plan”
DataValue = st.Attribute("value").Value // The Attribute named value (Case Sensitive) in the first case is “CP”
};


And then I return a list:
return stageTypes.ToList<StageTypeItem>();


Now I can bind a combobox to this list:
ComboBox cbo = new ComboBox();
cbo.DataSource = LoadStageType(“SUB”);
cbo.DisplayMember = “DataText”; // StageTypeItem.DataText
cbo.ValueMember = “DataValue”; // StageTypeItem.DataValue


Happy Coding!!!

1 comment:

十二月 said...

Hi, I'm trying to read from the excel file, but error below occurs:
'C:\Documents and Settings\hyna\Desktop\test1_partNo.xls' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.

It works fine in my local machine, however, error above occurs in the server.

string sFileName = FileUpload1.PostedFile.FileName.ToString();
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sFileName + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";

Is there something wrong with my codes? Error remain the same even i set impersonate="true".

Any idea?? Please assist. Thanks in advance.

Regards,
Howyi

 
Creative Commons License
Blogged Information and Code is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.