【问题标题】:How to place DisplayName into Variable如何将 DisplayName 放入变量
【发布时间】:2012-06-05 20:01:37
【问题描述】:

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

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

【问题讨论】:

标签: c# xml xml-serialization c#-2.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;
                    }
                }
            }
        }

【讨论】:

  • XmlReader reader = XmlReader.Create("\Xml\bin\UMSM.009.8Root.xml") 只是让程序崩溃
  • 我编辑了代码示例。您必须删除“Xml/...”前面的斜杠。该路径是相对于您的执行文件的。
  • 相对于可执行?好吧,该程序的所有可执行文件都存储在 C:\Proj\Run\bin\ 中,而所有 Xml 文件都在上面的 C:\Proj\Xml\bin\ 中,所以我将 proj\Xml\bin\UMSM... .?
  • 两个选项:1.) 使用绝对路径。或 2.) 从您的可执行文件中相对导航,在您的情况下为“../../Xml/bin/UMSM.009.8Root.xml”。您可以从 app.config-file 中读取路径以更灵活一些。
  • 不想问这么多问题,但是我该如何阅读我的 app.config 文件呢?
【解决方案2】:

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

有一个很好的例子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
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 2022-08-18
    • 2017-02-04
    • 2012-03-22
    • 2018-02-10
    • 2021-04-09
    • 2013-01-07
    相关资源
    最近更新 更多