如何访问XmlElementAttribute顺序

时间:2018-09-21 11:18:42

标签: c# xml wsdl

我引用了在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 个答案:

答案 0 :(得分:0)

我使用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;
            }
        }
    }
}