I’m trying to do something, that may be extremely simple so please bear with me, I just want to get ‘DisplayName’ from an XML file into a string in my C# code. here’s what I have:
THIS IS C#2.0 in VS2005
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.
In my C# code I’ve attempted the following but to No Avail:
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);
}
}
basically my current issue is that durring Debug the issue that I’m noticing is in the line where ‘reader’ is initialized… the program usually throws an exception here
You could use an XmlReader: