在不尊重Xml命名空间的情况下反序列化XML [英] Deserialize XML without respecting Xml Namespaces

查看:55
本文介绍了在不尊重Xml命名空间的情况下反序列化XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,



今天,我遇到了一个非常奇怪的问题,我没有找到任何解决方案:



假设我有一个看起来像这样的Xml文档:



 <   RootNode  >  
< ParentNode >
< 文本 = 测试 / >
< 文本 = Tes2t / >
< ; / ParentNode >
< ParentNode >
< 文字 value = 测试 / >
< < span class =code-leadattribute>文本 value = Tes2t / >
< < span class =code-leadattribute> / ParentNode
>
< / RootNode >





我有一个描述上述xml的XSD。通过xsd.exe拉出后,我得到了以下类(只有伪代码,没有我的代码):



 [XmlRoot(Namespace =   http://www.test.com/] 
public class RootNode
{
public ParentNode [] ParentNode { get ; set ;}
}

[XmlRoot(Namespace = http://www.test.com/]
public class ParentNode
{
public Text [] Text { get ; set ;}
}

[XmlRoot(Namespace = http:// ww w.test.com /]
public class 文字
{
public string 值{ get < /跨度>; set ; }
}





现在我尝试使用XmlSerializer反序列化上面的Xml(作为字符串)来获取对象结构。



主要问题是,在Xsd中,声明了名称空间,这些名称空间在Xml中不可用。我没有访问xml源来在那里添加xmlns属性,也不想从xsd.exe更改自动创建的类。



如果我使用XmlSerializer而没有任何其他设置,我得到一个错误,他在文档的开头有一个错误(他不希望< RootNode xmlns =节点)。如果我将RootNodeAttribute添加到XmlSerializer的构造函数中(在此处描述:http://msdn.microsoft.com/en-us/library/f1wczcys.aspx),则反序列化将在没有异常的情况下运行,但所有属性都有其默认值 - 值/空值)。



有没有人给我一个提示,我怎么能解决这个问题?我不介意我是否尊重xmlns,我只想将xml数据放入C#对象;)



更新1:

测试源位于: https://bitbucket.org/cke/xsdxmldeserializertest/overview [ ^ ]



更新2:

我最终为XML创建了一个新架构,因为我发现给定的XML与给定的XML不匹配xsd ...

有很多工作在等我...;)





非常感谢,任何帮助表示赞赏!

Chris

解决方案

它看起来像你的数据模型的C#代码(我不是说原始的XSD,因为我从未见过它)和XML样本不匹配,就这么简单。您可以轻松确认是否正在使用具有XML验证器的Visual Studio,您可以通过XML编辑器使用它。



-SA

我认为您正在寻找的是一种更改名称空间的方法,可以通过xsl转换完成。 Google为您提供了大量答案此处 [ ^ ] ......

Hey Guys,

Today, I experienced a very strange problem, and I did not find any solution for it:

Let's say I have a Xml Document which looks like this:

<RootNode>
  <ParentNode>
    <Text value="Test" />
    <Text value="Tes2t" />
  </ParentNode>
  <ParentNode>
    <Text value="Test" />
    <Text value="Tes2t" />
  </ParentNode>
</RootNode>



And I have an XSD which describes the above xml. After pulling it through xsd.exe, i got the following classes (only pseudocode, don't have the code with me):

[XmlRoot(Namespace = "http://www.test.com/"]
public class RootNode
{
  public ParentNode[] ParentNode { get; set; }
}

[XmlRoot(Namespace = "http://www.test.com/"]
public class ParentNode
{
  public Text[] Text { get; set; }
}

[XmlRoot(Namespace = "http://www.test.com/"]
public class Text
{
  public string Value { get; set; }
}



Now I try to deserialize the above Xml (as string) using the XmlSerializer to get the object structure.

The main Problem is, that in the Xsd, there are namespaces declared, which are not available in the Xml. I do not have access to the xml source to add the xmlns attributes there, nor do I want to change the automagically created classes from the xsd.exe.

If I use the XmlSerializer without any additional settings, I get an error that he have an error at the beginning of the document (he don't expected a <RootNode xmlns="" node). If I add a RootNodeAttribute to the constructor of the XmlSerializer (described here: http://msdn.microsoft.com/en-us/library/f1wczcys.aspx) then the deserialization runs whithout an exception, but all properties have their default-values / null values).

Does anyone have a hint for me, how I could solve this problem? It don't mind if I respect the xmlns or not, I just want to get the xml data into a C# object ;)

Update 1:
Testing source is here: https://bitbucket.org/cke/xsdxmldeserializertest/overview[^]

Update 2:
I ended up in creating a new schema for the XML, because I found out that the given XML does not match the given xsd...
Lots of work waiting for me... ;)


Thanks alot, any help is appreciated!
Chris

解决方案

It looks like your C# code of the data model (I'm not saying original XSD, as I never saw it) and the XML sample don't match, as simple as that. You can easily confirm is using Visual Studio which has an XML validator which you can use through the XML editor.

—SA


I think what you are looking for is a way to change namespaces, which can be done with an xsl transformation. Google has oodles of answers for you here[^]...


这篇关于在不尊重Xml命名空间的情况下反序列化XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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