English 中文(简体)
X 帝国主义失败
原标题:XmlSerializer Deserialize failures

我有第三方服务器的轮.。 Ran svcutil and end up wih a set of a set of a group

XmlNode AMethod(object Request);

方法。 另有100页,说明每种方法的反应/要求物体。

我的思想是总结网络方法,利用XmlSerializer来回重打的物体。 返回的Xml looks照(一除肥皂头盔):

<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:type="ResponseExt" 
        xmlns="http://www.thirdparty.com/lr/">
  <Code>0</Code>
  <Message>SUCCESS</Message>
  <SessionId>session_token</SessionId>
</Response>

简单。 创立一个班级(从文件/记录中):

[XmlRoot("Response")]
//EDIT added XmlType
[XmlType("ResponseExt", Namespace = "http://www.thirdparty.com/lr/")]
public class MyClass {
    public string Code {get; set;}
    public string Message {get; set;}
    public string SessionId {get; set;}
}

处理时间:

//XmlNode node = xml from above
XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
XmlNodeReader reader =  new XmlNodeReader(node);
Myclass myclass = serializer.Deserialize(reader) as MyClass

Last line is where it blows up with inner exception message: The specified type was not recognized: name= ResponseExt , namespace= http://www.thirdparty.com/lr/ , at <Response xmlns= >.
I can t figure out how to make Serializer happy and what exactly these two mean

xsi:type="ResponseExt" xmlns="http://www.thirdparty.com/lr/

一如既往地欢迎任何建议和协调人。


EDIT:接受的答复如下。

I was still getting exception, until i found this, hopefully it ll save someone some time. I started to work backwards. Captured xml on the wire. Deserialized to my created classes with correct attributes: worked like a charm. Tried again from webservice - exception. For some reason XmlSerializer doesn t recognize ResponseExt.

XmlSerializer serializer = new XmlSerializer(typeof(Response));
XmlNode node = (XmlNode)results[0];
XmlDocument doc = new XmlDocument();
doc.LoadXml(node.OuterXml); //reload node
XmlNodeReader reader = new XmlNodeReader(doc.FirstChild); //there is only one node
Response rsp = serializer.Deserialize(reader) as Response; //works

EDIT:基本问题卷宗不完整。 在为此花费了2天并找到了这段工作之后,第三方供应商提供了complete。 WSDL, 各种类型的电离层,无差错。

最佳回答

为什么当你有WSDL时,你人工淡化XML?

如果你拥有WSDL,则使用Svcutil.exe工具或 w。 外部工具,为XML电文的发送和接收提供代理班和DTO。

网上服务工具包或“快车”的目的是为你提供这种服务,这样你就不必手提课和XML序列化代码。

你们是否试图这样做? 你们是否试图通过这些工具之一来管理妇女发展基金? 或者,你在视觉演播室试图“补充网络参考资料”?


在更新这个问题后,我建议你修改《妇女发展权利宣言》,而不是撰写《习俗法典》。 您可以为这项服务生产一种习俗,而这种习俗将正确产生你想要的代理班。 如果你不需要所有100种方法(无论有多种方法),就会离开。 如果你想用某种方法标出一种习俗,那么就界定了一种复杂的习俗。 对应该物体的类型。 这比每个方法的手写的XML脱硫法更为简单、更可靠。


如果你不喜欢这种想法,并且希望坚持《葡萄牙王国法典》,那么你需要做两件事:

  1. a namespace to the XmlRootvide

  2. 将贵班的名称改为ResponseExt,并从一个称为Response的类别中取而代之。 http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlincludeattribute.aspx” rel=“nofollow noretinger”>XmlInclude。 这与使用.

本条在法典中考虑:

[XmlRoot("Response", Namespace="http://www.thirdparty.com/lr/")]
public class ResponseExt : Response {
}

[XmlRoot("Response", Namespace="http://www.thirdparty.com/lr/")]
[XmlInclude(typeof(ResponseExt))]
public class Response {
    public string Code {get; set;}
    public string Message {get; set;}
    public string SessionId {get; set;}
}

public class XsiType
{
    public static void Main(string[] args)
    {
        try
        {
            string filename = "XsiType.xml";
            XmlSerializer s1 = new XmlSerializer(typeof(Response));
            ResponseExt r = null;
            using(System.IO.StreamReader reader= System.IO.File.OpenText(filename))
            {
                r= (ResponseExt) s1.Deserialize(reader);
            }

            var builder = new System.Text.StringBuilder();
            var xmlws = new System.Xml.XmlWriterSettings { OmitXmlDeclaration = true, Indent= true };
            using ( var writer = System.Xml.XmlWriter.Create(builder, xmlws))
            {
                //s1.Serialize(writer, r, ns);
                s1.Serialize(writer, r);
            }
            string xml = builder.ToString();
            System.Console.WriteLine(xml);

        }
        catch (System.Exception exc1)
        {
            Console.WriteLine("Exception: {0}", exc1.ToString());
        }
    }
}

页: 1 我如何强迫使用Xsi:属性?

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签