我有一个问题,我无法摆脱它。我对 WebServices 的了解不是很好,我有一个小问题需要解决。 我正在为 Web 服务开发客户端,但我无权控制服务器端 Web 服务(我认为它是用 Java 开发的)。我使用 WSE3 作为我的客户端的基础,它似乎工作得很好,除了几个我无法解决的方法。 根据我的 WSDL 定义,我生成了我的代理类,其中包含要调用的正确数据类型和方法。其中许多方法返回已经反序列化的 SOAP 消息,转换为正确的对象类型。不幸的是,他们中的一些人发回了一个 ZIP 文件的字节数组,其中包含一个格式不正确的 xml 文件。我设法获取流、解压缩文件并读取 xml,但我无法正确反序列化 xml,然后将其转换为相应的类型。这是我的代码示例,也是我需要反序列化并转换为正确类型的 xml 示例。你有什么建议吗?
MyClient client = new MyClient(ServiceSettings);
ConnectResponseRetrieveMyType data;
try
{
// call web service method
data = client.syncData(service, startDate, endDate);
// unzip the byte array
using (ZipFile zip = ZipFile.Read(data.Data))
{
if (zip.ContainsEntry("data.xml"))
{
List<string> strings = new List<string>();
// read the xml file with multiple root elements
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;
using (XmlReader reader = XmlReader.Create(zip["data.xml"].OpenReader(), settings))
{
while (reader.Read())
{
strings.Add(reader.ReadOuterXml());
}
}
}
else
return "OGZIP01";
}
}
最后我有一个包含这些数据的字符串列表<>:
<c:CoverDecision TypeOfCover="CreditLimit" CoverId="123123123" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:c="http://atradius.com/connect/_2007_08/" xmlns:o="http://atradius.com/organisation/_2007_08/type/">
<Buyer>
<o:Identifier registeredOffice="SYMPH">
<o:id>123123123</o:id>
<o:countryTypeIdentifier>AUT</o:countryTypeIdentifier>
</o:Identifier>
<o:Identifier registeredOffice="COC">
<o:id>123123123F</o:id>
<o:countryTypeIdentifier>AUT</o:countryTypeIdentifier>
</o:Identifier>
<o:Name>
<o:name>SOME GES.M.B.H.</o:name>
<o:type>REG</o:type>
</o:Name>
<o:LegalForm>GMBH</o:LegalForm>
<o:Address>
<o:StreetDescription xsi:type="xsd:string">STRAßE 49</o:StreetDescription>
<o:City>FÜRSTENFELD</o:City>
<o:PostCode>23123</o:PostCode>
<o:CountryISOCode>AUT</o:CountryISOCode>
</o:Address>
</Buyer>
<Customer>
<o:Identifier registeredOffice="SYMPH">
<o:id>123123</o:id>
<o:countryTypeIdentifier>NLD</o:countryTypeIdentifier>
</o:Identifier>
<o:Identifier registeredOffice="COC">
<o:id>123123</o:id>
<o:countryTypeIdentifier>NLD</o:countryTypeIdentifier>
</o:Identifier>
<o:Name>
<o:name>SOME B.V.</o:name>
<o:type>REG</o:type>
</o:Name>
</Customer>
<PolicyId>123123</PolicyId>
<GenericApplication>
<CustomerReference>123123</CustomerReference>
<EntryDate>2010-02-04</EntryDate>
<Supersede>false</Supersede>
</GenericApplication>
<Decision>
<ApplicationResult>CreditLimitDecision</ApplicationResult>
<DecisionDate>2010-02-05</DecisionDate>
<EffectFrom>2010-02-05</EffectFrom>
<EffectTo>2010-07-19</EffectTo>
<CreditLimitDecision>
<CreditLimitResultCode>APPR</CreditLimitResultCode>
<DecisionCode>DC16</DecisionCode>
<FirstAmount>
<Amount>150000.00</Amount>
<Conditions>
<TypeOfConditions>ADMIN</TypeOfConditions>
<ConditionCode>T310</ConditionCode>
<ConditionText>Some condition description text.</ConditionText>
</Conditions>
</FirstAmount>
<SecondAmount>
<Amount>0</Amount>
</SecondAmount>
</CreditLimitDecision>
</Decision>
</c:CoverDecision>
而且我无法反序列化它并将其转换为正确的对象类型。我尝试了很多方法,但都没有成功。也许您有什么建议?
谢谢
最佳答案
是的,xml 没有格式化。 每个“标识符”元素都应该在“标识符”内。
除此之外,使用其他类型非常简单(对于标识符,在 Buyer 和 Consumer 类型中创建一个 List<Identifier>
。
读取数据最简单的方法是使用DataSet.ReadXml(xmlfile);
加载数据后,您将拥有一个包含“CoverDecision”、“Buyer”、“Identifier”等的表。它还会创建关系(dataSet.Relations
),在您的情况下是 13 个。
因此借助关系导航表格,您可以获得所有数据。
关于c# - 反序列化特定的 XML 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4773202/