代码之家  ›  专栏  ›  技术社区  ›  Michael Westwort

如何访问xmlementattribute顺序

  •  0
  • Michael Westwort  · 技术社区  · 7 年前

    我引用在WSDL文件中定义的web服务。WSDL文件使用序列,序列定义元素的特定顺序。在我的reference.cs文件中,正确地采用了以下顺序

    public class name {
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=0)]
        public string firstname {
            get; set;
        }
    }
    

    如何访问类名中成员名的顺序值?


    示例性WSDL文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://webaddress.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="WSDLService" targetNamespace="http://webaddress.com/">
        <wsdl:types>
            <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://webaddress.com/" elementFormDefault="unqualified" targetNamespace="http://webaddress.com/" version="1.0">
                <xs:complexType name="name">
                    <xs:sequence>
                        <xs:element name="firstname" type="xs:string"/>
                        <xs:element name="lastname" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:schema>
        </wsdl:types>
    </wsdl:definitions>
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   jdweng    7 年前

    我使用xsd.exe实用程序生成类。我不得不修改xsd,因为它没有通过验证。下面使用的代码生成一个xml文件,然后将其读回。

    架构如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://webaddress.com/" elementFormDefault="unqualified" targetNamespace="http://webaddress.com/" version="1.0">
      <xs:element name="name">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="firstname" type="xs:string"/>
            <xs:element name="lastname" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    

    代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;
    using System.IO;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                name Name = new name()
                {
                    firstname = "John",
                    lastname = "Smith"
                };
    
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                XmlWriter writer = XmlWriter.Create(FILENAME,settings);
    
                XmlSerializer serializer = new XmlSerializer(typeof(name));
                serializer.Serialize(writer,Name);
    
                writer.Close();
    
                XmlReader reader = XmlReader.Create(FILENAME);
    
                name readName = (name)serializer.Deserialize(reader);
    
            }
        }
        [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
        [System.SerializableAttribute()]
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://webaddress.com/")]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://webaddress.com/", IsNullable = false)]
        public partial class name
        {
    
            private string firstnameField;
    
            private string lastnameField;
    
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
            public string firstname
            {
                get
                {
                    return this.firstnameField;
                }
                set
                {
                    this.firstnameField = value;
                }
            }
    
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
            public string lastname
            {
                get
                {
                    return this.lastnameField;
                }
                set
                {
                    this.lastnameField = value;
                }
            }
        }
    }