我引用了一个在WSDL文件中定义的Web服务。WSDL 文件使用序列,这些序列定义了元素的特定顺序。在我的参考.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>
我使用 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:temptest.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;
}
}
}
}