我正在尝试做一些事情,这可能非常简单,所以请多多包涵,我只想将 XML 文件中的“DisplayName”转换为我的 C# 代码中的字符串。这就是我所拥有的:
这是 VS2005 中的 C#2.0
XML:
<MonitorScope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="System" xmlns="http://tempuri.org/XMLSchema.xsd">
<PersonalSafety>
<MonitorResponseRecord Enabled="false" DisplayName="ValveFailureAtCentralPosition">
<ExpressionMonitor>
<postAlarm>
<AlarmName>Valve_Position_Fault</AlarmName>
<Parameter1> Sensor Position = {X}</Parameter1>
<Parameter2> Sensor Position = {X}</Parameter2>
<Parameter3> Sensor Position = {X}</Parameter3>
</postAlarm>
</ExpressionMonitor>
</MonitorResponseRecord>
<MonitorResponseRecord ... ... ...>
... ...
... ... and so on about 1600 times.
在我的 C# 代码中,我尝试了以下但无济于事:C#:
public class AlarmRecord
{
/// <remarks/>
public string PmAlarm;
/// <remarks/>
public string Parameter1;
/// <remarks/>
public string Parameter2;
/// <remarks/>
public string Parameter3;
/// <remarks/>
public string DisplayName;
}
protected void OnPostAlarm(PostAlarm postAlarm)
{
try
{
AlarmRecord alarmRecord = new AlarmRecord();
alarmRecord.PmAlarm = postAlarm.AlarmName;
alarmRecord.Parameter1 = postAlarm.Parameter1;
alarmRecord.Parameter2 = postAlarm.Parameter2;
alarmRecord.Parameter3 = postAlarm.Parameter3;
string fileName = "UMSM.009.8Root.xml";
string fullPath;
fullPath = Path.GetFullPath(fileName);
XmlTextReader reader = new XmlTextReader(new StringReader(fullPath));
System.Xml.XPath.XPathDocument docNav = new System.Xml.XPath.XPathDocument(reader);
System.Xml.XPath.XPathNavigator Q = docNav.CreateNavigator();
System.Xml.XPath.XPathExpression EXE = Q.Compile("MonitorResponseRecord/@DisplayName");
alarmRecord.DisplayName = Convert.ToString(Q.Evaluate(EXE));
alarms.Enqueue( alarmRecord );
}
catch (Exception e)
{
Log.Write(e);
OnUnknownResponse(postAlarm);
}
}
基本上我当前的问题是在调试期间我注意到的问题是在初始化“阅读器”的行中......程序通常在这里抛出异常