0

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

基本上我当前的问题是在调试期间我注意到的问题是在初始化“阅读器”的行中......程序通常在这里抛出异常

4

2 回答 2

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;
                    }
                }
            }
        }
于 2012-06-05T21:10:08.220 回答
0

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

这里有一个不错的例子。尽管 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
            }
        }
于 2012-06-05T20:35:53.783 回答