读取现有XML文件时遇到一个奇怪的问题。我有一个以步骤为元素的XML文件。如果XML内有两个步骤,我想在一个列表框中显示它们。
我从XmlDoc中构建了一个List
var myStepList = new Steps(xmlDoc);
然后,我计算步骤数,如果内部有两个步骤,则转到案例2。
switch (myStepList.amountOfSteps)
{
case 1:
var StepData = myStepList.GetStepData(myStepList.stepList[0]);
ListBoxSteps.Items.Add(StepData[0].ToString());
TextBoxStepNo.Text = StepData[1].ToString();
TextBoxStepColorCode.Text = StepData[2].ToString();
break;
case 2:
var StepData1 = myStepList.GetStepData(myStepList.stepList[0]);
var StepData2 = myStepList.GetStepData(myStepList.stepList[1]);
ListBoxSteps.Items.Add(StepData1[0].ToString());
ListBoxSteps.Items.Add(StepData2[0].ToString());
TextBoxStepNo.Text = StepData1[1].ToString();
TextBoxStepNo2.Text = StepData2[1].ToString();
TextBoxStepColorCode.Text = StepData1[2].ToString();
TextBoxStepColorCode2.Text = StepData2[2].ToString();
break;
}
用正确的内容定义Stepdata2
时,它将覆盖StepData1
。我不知道为什么,因为在内容正确之前。
这是我的课程步骤:
class Steps
{
private List<XElement> StepList;
private readonly XAttribute[] StepData;
private int AmountOfSteps = 0;
private readonly int AmountOfStepAttributes = 3;
public Steps(XmlDoc xmlDoc)
{
StepList = xmlDoc.GetStepList();
StepData = new XAttribute[AmountOfStepAttributes];
}
public XAttribute[] GetStepData(XElement step)
{
StepData[0] = step.Attribute("name");
StepData[1] = step.Attribute("stepNo");
StepData[2] = step.Attribute("colorCode");
return StepData;
}
public List<XElement> stepList
{
get { return StepList; }
}
public int amountOfSteps
{
get
{
AmountOfSteps = stepList.Count();
return AmountOfSteps;
}
}
public int amountStepAttributes
{
get { return AmountOfStepAttributes; }
}
这是我的名为XmlDoc的课程:
class XmlDoc
{
private List<string> ListFileNames;
private XDocument XDoc;
private XAttribute RootAttribute;
private List<XElement> StepList;
public XmlDoc()
{
StepList = new List<XElement>();
ListFileNames = new List<string>();
}
public List<string> GetXmlFilesList(string path)
{
List<string> ListFileNames = new List<string>(Directory.GetFiles(path));
return ListFileNames;
}
public XDocument GetXDoc(string selectedXmlFile)
{
XDoc = XDocument.Load(selectedXmlFile);
return XDoc;
}
public XAttribute GetRootAttribute()
{
RootAttribute = XDoc.Root.Attribute("No");
return RootAttribute;
}
public List<XElement> GetStepList()
{
StepList = XDoc.Root.Elements("Step").ToList();
return StepList;
}
}
答案 0 :(得分:1)
当局部变量可用时,您正在类中使用实例字段。我将在Steps
中进行两个修复,将在XmlDoc
和其他类中解决类似的问题,由您来做:
class Steps
{
private List<XElement> StepList;
//Remove this, only needed inside GetStepData
//private readonly XAttribute[] StepData;
//Remove this, not needed
//private int AmountOfSteps = 0;
private readonly int AmountOfStepAttributes = 3;
public Steps(XmlDoc xmlDoc)
{
StepList = xmlDoc.GetStepList();
}
public XAttribute[] GetStepData(XElement step)
{
//Create a new array here
var StepData = new XAttribute[AmountOfStepAttributes];
StepData[0] = step.Attribute("name");
StepData[1] = step.Attribute("stepNo");
StepData[2] = step.Attribute("colorCode");
return StepData;
}
public List<XElement> stepList
{
get { return StepList; }
}
public int amountOfSteps
{
get
{
//Just directly return the count
return stepList.Count();
}
}
public int amountStepAttributes
{
get { return AmountOfStepAttributes; }
}
以上所有注释均应删除-它们仅描述更改的内容和原因。
在当前代码中,Steps
中包含一个数组,每次调用GetStepData
时,该数组都会被覆盖。在上面的固定代码中,每次调用新数组时,我们都会分配一个新数组,并且仅在该方法调用结束之前操作 数组。
一个不明显的提示-为您的项目打开选项“将警告作为错误处理”。简单的代码不应生成警告,它应该已经在您的XmlDocs
类中标记了至少一个问题(实例成员ListFileNames
已初始化但从未使用过)
OB链接Eric Lippert的How to debug small programs