如何将显示名称放入变量中

本文关键字:变量 显示 | 更新日期: 2023-09-27 18:36:17

我正在尝试做一些事情,这可能非常简单,所以请耐心等待,我只想将 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);
        }
    }

基本上我目前的问题是,在调试期间,我注意到的问题在初始化"读取器"的行中......程序通常会在这里抛出异常

如何将显示名称放入变量中

您可以使用 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;
                    }
                }
            }
        }

最好在 .NET 2.0 中使用 XPathNavigator 和 XPath 查询。读取属性(如示例中的显示名称)是通过在 XPath 查询中使用 @ 来完成的。

这里有一个很好的例子。尽管 XPathExpression 位不是必需的,但您可以调用 nav。直接计算并提供字符串 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
            }
        }