0

我正在尝试做一些事情,这可能非常简单,所以请多多包涵,我只想将 XML 文件中的属性转换为我的 C# 代码中的字符串。这就是我所拥有的:

XML:

<MonitorResponseRecord Enabled="false" DisplayName="ValveFailureAtCentralPosition">
        <ExpressionMonitor>
        <AlarmName>Valve_Position_Fault</AlarmName>
            <Parameter> Sensor Position = {X}</Parameter>
        </ExpressionMonitor>
</MonitorResponseRecord>
<MonitorResponseRecord ...  ... ...>
... ...
... ... and so on about 1600 times.

C#:

[Selrializable]
[XmlType("Alarm")]  
public class AlarmDefinition
    {
        public AlarmDefinition() {}
        public AlarmDefinition Clone()
        {
            AlarmDefinition clone = new AlarmDefinition();
            clone.DisplayName = DisplayName;
                          clone.Category = Category;
        clone.Color = Color;
        clone.Description = Description;
                          return clone;

                 }

    [XmlAttribute( ??????????? )] public string DisplayName = "";

所以有人知道我在'?????????????'中需要什么 部分?我试过了:

[XmlAttribute("MonitorResponseRecord",AttributeName = "DisplayName")] public string DisplayName = "";
4

1 回答 1

2

由于属性是@DisplayName,那要么就是:

[XmlAttribute]
public string DisplayName = "";

或者,更明确地说:

[XmlAttribute("DisplayName")]
public string DisplayName = "";

(虽然属性比公共领域更可取,IMO)

真正的问题似乎是根名称。它是 xml 文档的最外层,您需要:

[XmlRoot("MonitorResponseRecord")]
public class AlarmDefinition {...}
于 2012-05-31T20:16:39.550 回答