简体   繁体   English

在C#中解析XML字符串时获取Null值

[英]Getting Null value while parsing the XML string in C#

Trying to parse XML String in C# where in SessionId i am getting the null value. 尝试解析C#中的XML字符串,其中在SessionId中我获取了空值。 while pasing the value approx everything is coming correctly, just session id is null every time. 在输入值时,几乎所有操作都会正确进行,但每次会话ID均为null。 every other field is coming and being parsed very well. 其他所有字段都将很快被解析。 i just got the null value with the session id part. 我刚刚获得了带有会话ID部分的空值。 i tried to do this with JSON also but its not getting done. 我也尝试使用JSON来完成此操作,但未完成。 finally data.SessionId is null. 最后data.SessionId为null。

namespace EmailToActivityCreator
{

[DataContract]
public class EmailInfo
{
    [DataMember]
    public string Body { get; set; }

    [DataMember]
    public string CCMail { get; set; }

    [DataMember]
    public string EmailState { get; set; }

    [DataMember]
    public string FromEmail { get; set; }

    [DataMember]
    public string ToEmail { get; set; }

    [DataMember]
    public string Subject { get; set; }

    [DataMember]
    public string SessionId { get; set; }
}

class Program
{

    static void Main(string[] args)
    {
        String arguments = args[0];
        //Console.Error.Write(arguments);

        var bytes = Encoding.UTF8.GetBytes(arguments);
        EmailInfo data;
        using (var stream = new MemoryStream(bytes))
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(EmailInfo));
            data = (EmailInfo)serializer.ReadObject(stream);
        }

and the value we are passing is something like this 我们传递的价值是这样的

"<EmailInfo xmlns=\"http://schemas.datacontract.org/2004/07/ABC\">
<Body>&lt;div dir=&quot;ltr&quot;&gt;body
&lt;/div&gt;
</Body>
<CCMail></CCMail>
<EmailState>Received</EmailState>
<FromEmail>Ravi Ranjan &lt;ravi.ranjan@xyz.com&gt;</FromEmail>
<Subject>subject</Subject>
<ToEmail> &lt;ravi@eras.in&gt;</ToEmail>
<SessionId>sss</SessionId>
</EmailInfo>"

DataContractSerializer requires the elements in the XML to be in the same order as the data member order in the data contract . DataContractSerializer 要求 XML中的元素必须与数据协定中数据成员顺序相同。 Elements in the wrong order will be ignored ( reference 1 , reference 2 ). 错误顺序的元素将被忽略参考文献1参考文献2 )。 The default order is given here: Data Member Order . 默认顺序如下: 数据成员顺序

The basic rules for data ordering include: 数据排序的基本规则包括:

  • If a data contract type is a part of an inheritance hierarchy, data members of its base types are always first in the order. 如果数据协定类型是继承层次结构的一部分,则其基本类型的数据成员始终按顺序排列。

  • Next in order are the current type's data members that do not have the Order property of the DataMemberAttribute attribute set, in alphabetical order. 接下来的是当前类型的数据成员,这些成员没有按字母顺序设置DataMemberAttribute属性的Order属性。

  • Next are any data members that have the Order property of the DataMemberAttribute attribute set. 接下来是设置了DataMemberAttribute属性的Order属性的所有数据成员。 These are ordered by the value of the Order property first and then alphabetically if there is more than one member of a certain Order value. 这些按先后按Order属性的值排序,如果某个Order值中有多个成员,则按字母顺序排序。 Order values may be skipped. 订单值可能会被跳过。

Alphabetical order is established by calling the CompareOrdinal method. 通过调用CompareOrdinal方法来建立字母顺序。

You may specify the order in which the XML elements will be encountered using the DataMemberAttribute.Order property: 您可以使用DataMemberAttribute.Order属性指定遇到XML元素的顺序:

[DataContract(Namespace="http://schemas.datacontract.org/2004/07/ABC")] // The namespace you used.
public class EmailInfo
{
    [DataMember(Order = 1)]
    public string Body { get; set; }

    [DataMember(Order = 2)]
    public string CCMail { get; set; }

    [DataMember(Order = 3)]
    public string EmailState { get; set; }

    [DataMember(Order = 4)]
    public string FromEmail { get; set; }

    [DataMember(Order = 6)]
    public string ToEmail { get; set; }

    [DataMember(Order = 5)]
    public string Subject { get; set; }

    [DataMember(Order = 7)]
    public string SessionId { get; set; }
}

If your code is required to read XML with elements in any order, you must either implement IXmlSerializable yourself (a nuisance), or switch to another serializer such as XmlSerializer . 如果需要您的代码以任何顺序读取带有元素的XML,那么您必须自己实现IXmlSerializable (麻烦),或者切换到另一个序列化程序,例如XmlSerializer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM