我试图做一些事情,这可能非常简单,所以请耐心等待我,我只是想得到' DisplayName'从XML文件到我的C#代码中的字符串。这就是我所拥有的:
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);
}
}
基本上我当前的问题是在调试问题时我注意到的问题是“阅读器”和#39;阅读器&#39;初始化...程序通常会抛出异常
答案 0 :(得分:1)
您可以使用XmlReader:
protected void OnPostAlarm(PostAlarm postAlarm)
{
AlarmRecord record = null;
List<AlarmRecord> recordList = new List<AlarmRecord>();
using(XmlReader reader = XmlReader.Create("Xml/bin/UMSM.009.8Root.xml"))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "MonitorResponseRecord":
record = new AlarmRecord();
recordList.Add(record);
reader.MoveToAttribute("DisplayName");
record.DisplayName = reader.Value;
break;
case "AlarmName":
record.PmAlarm = reader.ReadString();
break;
case "Parameter1":
record.Parameter1 = reader.ReadString();
break;
case "Parameter2":
record.Parameter2 = reader.ReadString();
break;
case "Parameter3":
record.Parameter3 = reader.ReadString();
break;
}
}
}
}
答案 1 :(得分:0)
最好在.NET 2.0中使用XPathNavigator和XPath查询。 读取属性(如示例中的DisplayName)是通过在XPath查询中使用@来完成的。
有一个不错的例子here。虽然XPathExpression位不是必需的,但您可以直接调用nav.Evaluate并提供字符串XPath查询。
示例XML(我需要删除命名空间属性):
<MonitorScope>
<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>
</PersonalSafety>
使用XPath的代码示例:
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator nodeList = nav.Select("//MonitorScope/PersonalSafety/MonitorResponseRecord");
if (nodeList.Count != 0) {
foreach (XPathNavigator node in nodeList) {
// node queries are relative to MonitorResponseRecord node
string displayName = node.SelectSingleNode("./@DisplayName").Value;
string alarmName = node.SelectSingleNode("ExpressionMonitor/postAlarm/AlarmName").Value;
string param1 = node.SelectSingleNode("ExpressionMonitor/postAlarm/Parameter1").Value;
string param2 = node.SelectSingleNode("ExpressionMonitor/postAlarm/Parameter2").Value;
string param3 = node.SelectSingleNode("ExpressionMonitor/postAlarm/Parameter3").Value;
// do something with values
}
}