我的XML文件是這樣..儘量減少讀取XML的性能成本
<?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>
我需要閱讀該XML,並執行以下操作
- 如果驗證屬性是prsent檢查其值即0或1並執行一些操作
- 如果validate屬性不是prsent dnt,則不執行任何操作,即不檢查其值。
我試着寫下面的函數,但是它的性能代價要多得多。
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);
}
好的。那麼你到目前爲止嘗試過什麼?!我們不是來做你的功課。 –
這很好。你的問題是什麼?試圖完成這個任務時,你遇到過一些具體問題嗎?請記住,StackOverflow不是一家外包公司,您可以在其中發佈您的客戶/老闆/老師要求並接收源代碼作爲交換。 –
我試過上面寫的功能 – user1006897