My XML file is like..
<?xml version="1.0" encoding="utf-8" ?>
<Controls>
<Control ToValidate="0" ControlID="cmbTrialType" ControlType="FormControl" EngineValueID="" Enabled="0" Visible="1" Single="1" Input="1" Value="Superiority" ListInitValues="Superiority" FormulaEntered="" Caption="" IsValid="1" DecimalPlaces="" />
<Control ToValidate="1" ControlID="cmbTrialType" ControlType="FormControl" EngineValueID="" Enabled="1" Visible="0" Single="1" Input="1" Value="Superiority" ListInitValues="Superiority" FormulaEntered="" Caption="" IsValid="1" DecimalPlaces="" />
</Controls>
I need to read this xml and perform following operation
- If validate attribute is prsent check its value i.e. 0 or 1 and perform some action
- If validate attribute is not prsent dnt do anything i.e. dont check its value.
I tried writing following function but its performance cost is much more.
public void LoadXML(String xmlFileName)
{
Stopwatch sw = new Stopwatch();
sw.Start();
string refernceFileName = xmlFileName;
XmlTextReader textReader = new XmlTextReader(refernceFileName);
//int count = 0;
// Read until end of file
while (textReader.Read())
{
XmlNodeType nType = textReader.NodeType;
// if node type is an element
if (nType == XmlNodeType.Element)
{
if (textReader.Name.Equals("Control"))
{
if (textReader.AttributeCount >= 1)
{
String val = string.Empty;
val = textReader.GetAttribute("Visible");
if (!(val == null || val.Equals(string.Empty)))
{
int choice = Int32.Parse(val);
switch (choice)
{
case 0: Console.WriteLine("Visible");
break;
case 1: Console.WriteLine("Not Visible");
break;
}
}
}
}
}
}
sw.Stop();
Console.WriteLine("Elapsed={0}", sw.Elapsed);
//Console.WriteLine(count);
}
Try this: