简体   繁体   English

使用XmlAttribute()在C#visual2005中检索

[英]use of XmlAttribute() to retrieve in C# visual2005

I'm trying to do something, that may be extremely simple so please bear with me, I just want to get an Attribute from an XML file into a string in my C# code. 我正在尝试做一些事情,这可能非常简单,所以请耐心等待,我只是想从XML文件中获取一个属性到我的C#代码中的字符串。 here's what I have: 这就是我所拥有的:

XML: 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#: 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 = "";

so does anyone know what I would need in the '???????????' 所以有人知道我需要什么'???????????? section? 部分? I've tried: 我试过了:

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

Since the attribute is @DisplayName , that is either just: 由于该属性是@DisplayName ,因此它只是:

[XmlAttribute]
public string DisplayName = "";

or, more explicitly: 或者,更明确地说:

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

(although a property would be preferable to a public field, IMO) (虽然财产比公共领域更可取,IMO)

The real problem seems to be the root name; 真正的问题似乎是根名称; it that is the outermost part of the xml document, you will need: 它是xml文档的最外层部分,您将需要:

[XmlRoot("MonitorResponseRecord")]
public class AlarmDefinition {...}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM