-1

VS2010 で XSD.exe によって作成されたクラスを取得しました。

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.269
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.0.30319.1.
// 

namespace ConsoleApplication1
{
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class transfer
    {

        private string sourceGLNField;

        private string destinationGLNField;

        private string actionTypeField;

        private string shipToField;

        private string documentNumberField;

        private string documentDateField;

        private string noteField;

        private string versionField;

        private transferCarrier[] carrierField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string sourceGLN
        {
            get
            {
                return this.sourceGLNField;
            }
            set
            {
                this.sourceGLNField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string destinationGLN
        {
            get
            {
                return this.destinationGLNField;
            }
            set
            {
                this.destinationGLNField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string actionType
        {
            get
            {
                return this.actionTypeField;
            }
            set
            {
                this.actionTypeField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string shipTo
        {
            get
            {
                return this.shipToField;
            }
            set
            {
                this.shipToField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string documentNumber
        {
            get
            {
                return this.documentNumberField;
            }
            set
            {
                this.documentNumberField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string documentDate
        {
            get
            {
                return this.documentDateField;
            }
            set
            {
                this.documentDateField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string note
        {
            get
            {
                return this.noteField;
            }
            set
            {
                this.noteField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string version
        {
            get
            {
                return this.versionField;
            }
            set
            {
                this.versionField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("carrier", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public transferCarrier[] carrier
        {
            get
            {
                return this.carrierField;
            }
            set
            {
                this.carrierField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class transferCarrier
    {

        private transferCarrierProductList[] productListField;

        private string carrierLabelField;

        private string containerTypeField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("productList", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public transferCarrierProductList[] productList
        {
            get
            {
                return this.productListField;
            }
            set
            {
                this.productListField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string carrierLabel
        {
            get
            {
                return this.carrierLabelField;
            }
            set
            {
                this.carrierLabelField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string containerType
        {
            get
            {
                return this.containerTypeField;
            }
            set
            {
                this.containerTypeField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class transferCarrierProductList
    {

        private transferCarrierProductListSerialNumber[] serialNumberField;

        private string expirationDateField;

        private string lotNumberField;

        private string gTINField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("serialNumber", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = true)]
        public transferCarrierProductListSerialNumber[] serialNumber
        {
            get
            {
                return this.serialNumberField;
            }
            set
            {
                this.serialNumberField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string expirationDate
        {
            get
            {
                return this.expirationDateField;
            }
            set
            {
                this.expirationDateField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string lotNumber
        {
            get
            {
                return this.lotNumberField;
            }
            set
            {
                this.lotNumberField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string GTIN
        {
            get
            {
                return this.gTINField;
            }
            set
            {
                this.gTINField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class transferCarrierProductListSerialNumber
    {

        private string valueField;

        /// <remarks/>
        [System.Xml.Serialization.XmlTextAttribute()]
        public string Value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class NewDataSet
    {

        private transfer[] itemsField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("transfer")]
        public transfer[] Items
        {
            get
            {
                return this.itemsField;
            }
            set
            {
                this.itemsField = value;
            }
        }
    }

}

また、次のようなコンソール アプリケーションを取得しました。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Reflection;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load xml file
            using (FileStream xmlStream = new FileStream("C:\\SaleReturn01.xml", FileMode.Open))
            {
                //Read xml file
                using (XmlReader xmlReader = XmlReader.Create(xmlStream))
                {
                    //Serialize xml with class which is created by XSD.exe in visual studio and set class with xmlfile
                    //In that case we don't need to xsd referenced validation
                    XmlSerializer serializer1 = new XmlSerializer(typeof(transfer));
                    transfer deserializedStudents = serializer1.Deserialize(xmlReader) as transfer;
                    showTransferClass(deserializedStudents);
                }
            }
        }

        public static void showTransferClass(transfer x)
        {
            try
            {
                FieldInfo[] myFields = typeof(transfer).GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
                foreach (FieldInfo field in myFields)
                {
                    if (!field.FieldType.IsArray)
                    {
                        Console.WriteLine("{0} : {1}", field.Name, field.GetValue(x));
                    }
                    else
                    {
                        Console.WriteLine("{0} : {1}", field.Name, "");
                        foreach (transferCarrier y in x.carrier)
                        {
                            showTransferCarierClass(y);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception : {0}", e.Message);
            }
        }

        public static void showTransferCarierClass(transferCarrier x)
        {
            try
            {
                FieldInfo[] myFields = typeof(transferCarrier).GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
                foreach (FieldInfo field in myFields)
                {
                    if (!field.FieldType.IsArray)
                    {
                        Console.WriteLine("  {0} : {1}", field.Name, field.GetValue(x));
                    }
                    else
                    {
                        Console.WriteLine("  {0} : {1}", field.Name, "");
                        foreach (transferCarrierProductList y in x.productList)
                        {
                            showTransferCarierProductListClass(y);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("  Exception : {0}", e.Message);
            }
        }

        public static void showTransferCarierProductListClass(transferCarrierProductList x)
        {
            try
            {
                FieldInfo[] myFields = typeof(transferCarrierProductList).GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
                foreach (FieldInfo field in myFields)
                {
                    if (!field.FieldType.IsArray)
                    {
                        Console.WriteLine("    {0} : {1}", field.Name, field.GetValue(x));
                    }
                    else
                    {
                        Console.WriteLine("    {0} : {1}", field.Name, "");
                        foreach (transferCarrierProductListSerialNumber y in x.serialNumber)
                        {
                            showTransferCarierProductListSerialNumberClass(y);

                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("    Exception : {0}", e.Message);
            }
        }

        public static void showTransferCarierProductListSerialNumberClass(transferCarrierProductListSerialNumber x)
        {
            try
            {
                FieldInfo[] myFields = typeof(transferCarrierProductListSerialNumber).GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
                foreach (FieldInfo field in myFields)
                {
                    if (!field.FieldType.IsArray)
                    {
                        Console.WriteLine("      {0} : {1}", field.Name, field.GetValue(x));
                    }
                    else
                    {
                        Console.WriteLine("      {0} : {1}", field.Name, "");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("      Exception : {0}", e.Message);
            }
        }
    }
}

全体として、フィールドの名前を取得できますが、フィールドの値を取得しようとするたびに、そのような例外がスローされました。今、私は真のコードと動作を手に入れました.(Jon Skeetに感謝します)

このコードを改善するアイデアはありますか?

4

1 に答える 1

1

そうです...あなたは電話しています:

temp = fld.GetValue(null);

のドキュメントからFieldInfo.GetValue

フィールドが static の場合、obj は無視されます。非静的フィールドの場合、obj は、フィールドを継承または宣言するクラスのインスタンスである必要があります。

では、フィールドを表示しようとしているタイプのインスタンスは何ですか? それを引数として に渡しますGetValue。インスタンスがなければ、「フィールドの値」という概念は意味がありません。String.Length「の値は?」と言っているようなものです。特定の文字列を参照せずに。

(また、.NET 命名規則に従っていれば、コードがより明確になることにも注意してください。また、変数を最初に使用する時点でのみ宣言して、そのスコープを制限した場合も同様です。)

于 2012-08-22T13:16:29.597 に答える