【问题标题】:How to access values in XmlElementAttributte in .NET如何在 .NET 中访问 XmlElementAttribute 中的值
【发布时间】:2015-06-18 06:33:20
【问题描述】:

我在 .NET 中调用一个 Web 服务操作,它返回带有以下类对象的 xml 数据:

    public partial class data : object, System.ComponentModel.INotifyPropertyChanged {

    private object[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("currentRow", typeof(dataCurrentRow), Order=0)]
    [System.Xml.Serialization.XmlElementAttribute("deleteRow", typeof(dataDeleteRow), Order=0)]
    [System.Xml.Serialization.XmlElementAttribute("insertRow", typeof(dataInsertRow), Order=0)]
    [System.Xml.Serialization.XmlElementAttribute("modifyRow", typeof(dataModifyRow), Order=0)]
    public object[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
            this.RaisePropertyChanged("Items");
        }
    }



    public partial class dataCurrentRow : object, System.ComponentModel.INotifyPropertyChanged {

    private object[] columnValueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("columnValue", Order=0)]
    public object[] columnValue {
        get {
            return this.columnValueField;
        }
        set {
            this.columnValueField = value;
            this.RaisePropertyChanged("columnValue");
        }
    }

从 Web 服务调用返回的 XML:

      <QAS_GETQUERYRESULTS_RESP_MSG xmlns="http://xmlns.oracle.com/Enterprise/Tools/schemas/QAS_GETQUERYRESULTS_RESP_MSG.VERSION_1">
     <webRowSet xmlns="http://java.sun.com/xml/ns/jdbc">
        <properties>
    ...
    ...
        </properties>
        <metadata>
    ...
    ...
        </metadata>
        <data>
           <currentRow>
              <columnValue>abc</columnValue>
              <columnValue>123</columnValue>
              <columnValue>xyz</columnValue>
           </currentRow>
           <currentRow>
              <columnValue>def</columnValue>
              <columnValue>456</columnValue>
              <columnValue>opq</columnValue>
           </currentRow>
        </data>
     </webRowSet>
  </QAS_GETQUERYRESULTS_RESP_MSG>

但是,我不确定如何使用类对象“data”访问 .NET 中的 xml 值“columnValue”。请帮助向我展示如何访问这些值。非常感谢!

【问题讨论】:

    标签: c# .net xml web-services xml-parsing


    【解决方案1】:

    没有所有代码就不容易回答,但是您应该能够迭代您的“项目”集合,检查每个集合的类型。如果您找到一个“typeof(dataCurrentRow)”项目,那么您应该能够将该对象转换为该类型并访问其属性集合(我希望它是一个“columnValue”对象的集合。你有另一个部分类中的 dataCurrentRow 对象,如果你可以发布这个我可以给你一个例子。

    编辑 - 您可以使用列值(请注意,您需要更改对象名称空间以与您自己的名称一致) -

            var myData = new data();
            //populate the data object via your webservice call.
    
            if (myData.Items != null && myData.Items.Length > 0)
            {
                var currentData = from c in myData.Items where c.GetType() == typeof(ConsoleApplication3.data.dataCurrentRow) select c as ConsoleApplication3.data.dataCurrentRow ;
    
                if (currentData != null && currentData.Count() > 0)
                {
                    foreach (var row in currentData)
                    {
                        if(row != null && row.columnValue != null)
                            Console.WriteLine(row.columnValue);
                    }
                }
            }
    

    【讨论】:

    • 非常感谢您的回复。我已将部分类 dataCurrentRow 添加到我的问题中。谢谢。
    • @Terry - 在我的原始答案中添加了示例 - HTH
    • 非常感谢!会试试看。
    • 我测试了这个例子,效果很好!非常感谢您的指导:)
    • 没问题,很高兴它有帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 2022-11-14
    • 2020-09-01
    • 1970-01-01
    相关资源
    最近更新 更多