i make a wpf application its running well.but whenever the size of my xml is to big its running very slow every time we fetch data from xml as code below is any body suggest me it is because of this or may be other problem
how can i reform this thanks shashank`
DataSet xmlData = new DataSet();
XmlTextReader reader = null;
try
{
if (File.Exists(CommonUtils.xmlPath))
{
//convert XmlDocument to XmlTextReader
reader = new XmlTextReader(new StringReader(CommonUtils开发者_StackOverflow.DecryptXML().OuterXml));
//get the xml data
xmlData.ReadXml(reader);
reader.Close();
//get category rows from
DataRow[] eventRows = xmlData.Tables["M_EVENT"].Select(" ROW_STATUS=1");
if (eventRows.Length > 0)
{
//create a datatable for event
DataTable dtEvent = xmlData.Tables["M_EVENT"].Clone();
//add a default row to the event table
DataRow dr = dtEvent.NewRow();
dr[0] = "-1";
dr[1] = "--Select Event--";
dr[2] = "1";
dtEvent.Rows.InsertAt(dr, 0);
foreach (DataRow row in eventRows)
{
DataRow drEvent = dtEvent.NewRow();
drEvent["PK_EVENT_ID"] = row["PK_EVENT_ID"];
drEvent["EVENT_NAME"] = row["EVENT_NAME"];
drEvent["EVENT_TYPE"] = row["EVENT_TYPE"];
dtEvent.Rows.Add(drEvent);
}
//bind the category drop down
cmbEvent.DataContext = dtEvent.DefaultView;
cmbEvent.SelectedValuePath = "PK_EVENT_ID";
cmbEvent.DisplayMemberPath = "EVENT_NAME";
cmbEvent.SelectedIndex = 0;
}
}
else
{
Lblgetevent.Visibility = Visibility.Visible;
}
}`
Ouch!
What are you using DataTable for?! It is terribly inefficient for this purpose and requires you to write lots of extra code. Also, why are you setting ComboBox properties from code and not in XAML?
A much, much simpler way is to bind your ComboBox directly to the XML:
<ComboBox ItemsSource="{Binding EventXml, XPath=M_EVENT[ROW_STATUS=1]}"
...>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding XPath=EVENT_NAME}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
This is better than the DataTable technique, but to add your "Select Event" row will require a CompositeCollection to be added. That can be done, but...
The best solution is to use LINQ to XML:
public object Events
{
get
{
return new[] { new { Name = "--Select Event--", Id = -1 }}.Concat(
from element in EventsXml.Elements("M_EVENT")
where element.Element("ROW_STATUS").Value=="1"
select new
{
Name = element.Element("EVENT_NAME").Value,
Id = int.Parse(element.Element("PK_EVENT_ID").Value),
});
}
}
With this simple XAML:
<ComboBox ItemsSource="{Binding Events}" SelectedValuePath="Id" ...>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
The LINQ to XML solution is faster than binding to XML, which in turn is faster than using DataTable. Not only that, but the LINQ to XML solution is much cleaner than the other two.
精彩评论