C# Can';t使用DataContractSerializer反序列化xml

C# Can';t使用DataContractSerializer反序列化xml,c#,xml,asp.net-core,datacontractserializer,C#,Xml,Asp.net Core,Datacontractserializer,我无法将此XML反序列化为对象,我不知道这有什么问题: <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <ProcessOneWayEvent xmlns="http://schemas.microsoft.com/sharepoint/remoteapp/"> <properties xml

我无法将此XML反序列化为对象,我不知道这有什么问题:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
    <ProcessOneWayEvent xmlns="http://schemas.microsoft.com/sharepoint/remoteapp/">
        <properties xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <CultureLCID>1033</CultureLCID>
        </properties>
    </ProcessOneWayEvent>
</s:Body>
</s:Envelope>

1033
这是一个准备好的解决方案,因为我不能在请求中修改,所以我的模型有什么问题吗?是否有任何解决方案可以在不使用XmlSerializer的情况下反序列化XML


我不知道为什么它不起作用,但有一种选择是使用
System.Xml.Serialization.XmlSerializer

首先,通过将XML复制到一个新类(编辑)中来创建适当的信封类→ 特殊粘贴→ 将XML粘贴为类)

然后以此为例进行反序列化:

byte[]byteArray=System.Text.Encoding.UTF8.GetBytes(请求);
使用(var流=新内存流(byteArray))
{
var serializer=新的XmlSerializer(typeof(信封));
信封响应=(信封)序列化程序。反序列化(流);
WriteLine(JsonConvert.SerializeObject(response));
}

我现在不明白为什么它不起作用,但有一种选择是使用
System.Xml.Serialization.XmlSerializer

首先,通过将XML复制到一个新类(编辑)中来创建适当的信封类→ 特殊粘贴→ 将XML粘贴为类)

然后以此为例进行反序列化:

byte[]byteArray=System.Text.Encoding.UTF8.GetBytes(请求);
使用(var流=新内存流(byteArray))
{
var serializer=新的XmlSerializer(typeof(信封));
信封响应=(信封)序列化程序。反序列化(流);
WriteLine(JsonConvert.SerializeObject(response));
}
尝试以下操作:

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)
        {
            string xml = File.ReadAllText(FILENAME);
            StringReader sReader = new StringReader(xml);
            XmlReader reader = XmlReader.Create(sReader);

            XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
            Envelope envelope = (Envelope)serializer.Deserialize(reader);

        }
    }
    [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body { get; set; }
    }
    public class Body
    {
        [XmlElement(ElementName = "ProcessOneWayEvent", Namespace = "http://schemas.microsoft.com/sharepoint/remoteapp/")]
        public ProcessOneWayEvent ProcessOneWayEvent { get; set; }
    }
    public class ProcessOneWayEvent
    {
        public Properties properties { get; set; } 
    }
    public class Properties
    {
        public string CultureLCID { get; set; }
    }
}

使用Xml-Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{

    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string xml = File.ReadAllText(FILENAME);
            XDocument doc = XDocument.Parse(xml);
            string CultureLCID = (string)doc.Descendants().Where(x => x.Name.LocalName == "CultureLCID").FirstOrDefault();


        }
    }

}
请尝试以下操作:

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)
        {
            string xml = File.ReadAllText(FILENAME);
            StringReader sReader = new StringReader(xml);
            XmlReader reader = XmlReader.Create(sReader);

            XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
            Envelope envelope = (Envelope)serializer.Deserialize(reader);

        }
    }
    [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body { get; set; }
    }
    public class Body
    {
        [XmlElement(ElementName = "ProcessOneWayEvent", Namespace = "http://schemas.microsoft.com/sharepoint/remoteapp/")]
        public ProcessOneWayEvent ProcessOneWayEvent { get; set; }
    }
    public class ProcessOneWayEvent
    {
        public Properties properties { get; set; } 
    }
    public class Properties
    {
        public string CultureLCID { get; set; }
    }
}

使用Xml-Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{

    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string xml = File.ReadAllText(FILENAME);
            XDocument doc = XDocument.Parse(xml);
            string CultureLCID = (string)doc.Descendants().Where(x => x.Name.LocalName == "CultureLCID").FirstOrDefault();


        }
    }

}

Body
spremoteventproperties
需要在
中http://schemas.microsoft.com/sharepoint/remoteapp/“
名称空间:

[DataContract(Name = "Body", Namespace = "http://schemas.microsoft.com/sharepoint/remoteapp/")]
public class Body
{
    [DataMember(Name = "ProcessOneWayEvent")]
    public ProcessOneWayEvent ProcessOneWayEvent;
}

[DataContract(Name = "properties", Namespace = "http://schemas.microsoft.com/sharepoint/remoteapp/")]
public class SPRemoteEventProperties
{
    [DataMember(Name = "CultureLCID") ]
    public int CultureLCID { get; set; }
}
控制数据协定对象的数据成员元素序列化到的命名空间,以及当数据协定对象是根元素时根元素的命名空间。由于元素
声明了一个新的默认XML名称空间,因此元素本身及其子元素都位于该名称空间中。因此,包含数据的契约对象
Body
必须相应地设置其数据成员名称空间。至于
i:
名称空间不是默认名称空间,因此实际上没有元素分配给该名称空间,
spremoteventproperties
类型不应该将自身分配给它


固定小提琴。

主体
SPRemoteEventProperties
需要在
中。”http://schemas.microsoft.com/sharepoint/remoteapp/“
名称空间:

[DataContract(Name = "Body", Namespace = "http://schemas.microsoft.com/sharepoint/remoteapp/")]
public class Body
{
    [DataMember(Name = "ProcessOneWayEvent")]
    public ProcessOneWayEvent ProcessOneWayEvent;
}

[DataContract(Name = "properties", Namespace = "http://schemas.microsoft.com/sharepoint/remoteapp/")]
public class SPRemoteEventProperties
{
    [DataMember(Name = "CultureLCID") ]
    public int CultureLCID { get; set; }
}
控制数据协定对象的数据成员元素序列化到的命名空间,以及当数据协定对象是根元素时根元素的命名空间。由于元素
声明了一个新的默认XML名称空间,因此元素本身及其子元素都位于该名称空间中。因此,包含数据的契约对象
Body
必须相应地设置其数据成员名称空间。至于
i:
名称空间不是默认名称空间,因此实际上没有元素分配给该名称空间,
spremoteventproperties
类型不应该将自身分配给它


修复了此问题。

谢谢你,贝伦德,但我提到,由于性能原因,我无法使用XmlSerializerissue@Houssem那么,最好更新您的问题并明确提及此要求:-(谢谢你,贝伦德,但我提到,由于性能原因,我不能使用XmlSerializerissue@Houssem那么,最好更新您的问题并明确提及此要求:-(我发布了两个解决方案。一个使用序列化程序,一个使用xml linqI发布了两个解决方案。一个使用序列化程序,一个使用xml linqYou真的应该在问题本身中包含您的代码,而不仅仅是在外部小提琴中。请参阅:是否可以创建一个可以链接到的问题的实时示例(例如,on或)然后这样做——但也要将代码复制到问题本身。不是每个人都可以访问外部网站,链接可能会随着时间的推移而中断。你真的应该将代码包含在问题本身中,而不仅仅是包含在外部文件中。请参阅:是否可以创建一个可以链接到的问题的实例(例如,on或)然后这样做-但也复制到问题本身的代码。不是每个人都可以访问外部网站,链接可能会随着时间的推移而中断。谢谢,这真是一个伟大的解决方案和解释。谢谢,这真是一个伟大的解决方案和解释。