【问题标题】:How to access the XmlElementAttribute order如何访问 XmlElementAttribute 顺序
【发布时间】:2018-09-21 11:18:41
【问题描述】:

我引用了一个在 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>

【问题讨论】:

  • 你需要使用序列化吗?你能用 Xml Linq 来解析吗?
  • 好吧,我创建了一个类name的实例myName,它有一个成员myName.firstname。我需要知道那个成员的顺序。将每个成员与其在解析的 XML 文件中的出现进行匹配是很麻烦的。如果这是唯一的解决方案,我知道该怎么做。
  • 您能否发布一个 Xml 示例以便我更好地理解这个问题?我通常喜欢使用字典,因此字典的键是名称,值是 List.
  • 我添加了一个示例性的 WSDL 文件。该文件会自动解析为 C# 文件reference.cs,其中包含所有可以访问的类定义。 sequence 元素导致XmlElementAttribute order = 0 代表名字,1 代表姓氏。所以实际上,order = 0 和 order = 1 并没有包含在 WSDL 文件中。

标签: c# xml wsdl


【解决方案1】:

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

【讨论】:

  • 好的,谢谢。但是我看不到在哪里可以检索有关order 属性值的信息。我需要的是一个函数,它为对象firstname 或字符串"firstname" 返回一个零值,为lastname 返回一个值。
  • 你为什么在乎?您可以使用属性获取元素。如果您确实需要索引,则需要使用自定义序列化方法。
猜你喜欢
  • 2015-06-18
  • 2017-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-03
  • 2021-12-31
相关资源
最近更新 更多