如何将XML字符串转换为C#对象 [英] How do I convert XML string to C# object

查看:686
本文介绍了如何将XML字符串转换为C#对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我将xml值设为null kinldy help。



Hi i'm getting the xml values as null kinldy help.

public logistics GetGeneralInfo(int StoreId)
       {
           var storeInfoWebService = new UATStoreReportServiceReference.oraclemdb1SoapClient("oraclemdb1Soap");
           var logisticsInfoFromService = storeInfoWebService.GetLogistics(StoreId.ToString());
           XmlSerializer serializer = new XmlSerializer(typeof(logistics));
           StringReader rdr = new StringReader(logisticsInfoFromService);
           logistics resultingMessage = (logistics)serializer.Deserialize(rdr);
           return resultingMessage;

       }





这是我的变量为logisticsInfoFromService的XML字符串





This is my XML string coming in variable logisticsInfoFromService as

<results>
<response mode="all" value="" />
<logistics>
<freight>
rdc1="01 - HARVARD" 
rdc2="01 - HARVARD" 
freightrate="0 %" 
offshoredelivery="Y" 
stratcode="Y" 
multipledelivery="N" 
palletized="N" 
totesshipped="0" 
totesreturned="0" 
toteslastdate="5/30/2001 12:00:00 AM" 
containersshipped="0"
containersreturned="0" 
containerslastdate="5/30/2001 12:00:00 AM"/>
<routes />
</freight></logistics>
</results>





我将运费和OffshreDelivery的值作为NULL获取



我尝试过的事情:



我试过反序列化数据,但它发送的值为NULL



I'm gettting the values for freightrate and OffshreDelivery as NULL

What I have tried:

I have tried by deserializing the data but it was sending the values as NULL

推荐答案

问题可能是你的XML file不是XML文件。



否则,使用调试器l向您展示究竟发生了什么。
The problem may be that your XML file is not an XML file.

Otherwise, using the debugger will show you exactly what is going on.


XML的结构表明它的架构确实是一个坏主意。它几乎是XML内部的自制文本。







等一下......这根本不是XML文件。它甚至没有良好的形成; ppolymorphe在解决方案1中引起了我的注意。嗯,你知道......没有什么可讨论的。



[结束编辑]



我建议停止使用过时且丑陋的 XmlSerializer 并切换到一个非常好的东西,数据合同

使用数据合同 [ ^ ]。



它更强大,更易于使用。首先,您不必自己处理任何特定于XML的内容。您只需创建数据类并告诉序列化器序列化的内容和不序列化的内容;它也是一种完全非侵入式和数据模型无关的技术。



-SA
The structure of your XML shows that it's schema is really a bad idea. It's pretty much plain home-baked text inside XML.



Wait a minute… This is not XML file at all. It's not even well-formed; ppolymorphe brought it to my attention in Solution 1. Well, you know… there is nothing to discuss.

[END EDIT]

I would advise stop using obsolete and ugly XmlSerializer and switch to a really good thing, Data Contract:
Using Data Contracts[^].

It's much more robust and easier to use. First of all, you don't have to deal with anything XML-specific by yourself. You just create your data classes and tell the serializer what to serialize and what not; it's a totally non-intrusive and data model agnostic technology, too.

—SA


可能是从xml到对象的转换没有正确发生。您有权使用物流课程吗?您能否检查Deserializer如何将您的货运和OffshreDelivery属性从xml反序列化到对象或它们的类型?我的意思是运费和OffshreDelivery属性的类型是什么,他们是否期望返回的值?
May be the conversion from the xml to the object is not happening properly. Do you have access to the logistics class? Can you check how the Deserializer deserializes your freightrate and OffshreDelivery properties from the xml to object or what ever type they are? by that i mean what is the type of freightrate and OffshreDelivery properties and are they expecting the values that are returned?


这篇关于如何将XML字符串转换为C#对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆