“生成样本XML” - 我想重用名称空间 [英] "Generate Sample XML" -- I want reused namespaces

查看:70
本文介绍了“生成样本XML” - 我想重用名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用"Generate Samle XML"时在Visual Studio 2010中的XML Schema Explorer的上下文菜单上,它生成如下XML:

When I use the "Generate Samle XML" option on the context menu of the XML Schema Explorer in Visual Studio 2010 it generates XML like this:


<Root>
<No xmlns="cddd-vw-ext">1</No>
<Type xmlns="cddd-vw-ext">G</Type>
...
</Root>

推荐答案

您好,

这取决于您的架构。生成XML的代码不会整体看整个事物,它一次只是元素。因此,在上面的示例中,每个元素都需要一个单独的命名空间声明,因为父(Root)不会声明它需要的
命名空间。基本上它必须向前看并猜测你将需要什么名称空间并在根上声明那些名称空间。给出一个单独的文档对人们来说很明显,但编写一个适用于所有文档
的通用算法非常困难,主要是因为两个人对于它应该是什么样子会有两种不同的看法。

It depends on your schema. The code generating the XML does not look at the entire thing as a whole, it simply goes element at a time. So in your sample above, each element needs a separate namespace declaration, cause the parent (Root) doesn't declare the namespace it needs. Basically it would have to look ahead and guess what namespaces you're going to need and declare those on the root. Given a single document it's kind of obvious for people, but writing a generic algorithm which would work on all documents is very hard, mainly because two people will have two different opinions about how it should look like.

如果您对文档有一定了解,可以对文档进行后期处理。一种方法是将文档加载到XLinq(不存储前缀),从中删除所有名称空间声明属性(非常简单),并在根元素上为文档命名空间添加名称空间声明
需要。然后保存它。 XmlWriter将为您重用根元素的声明。

You can post-process the document if you have some knowledge about it. One way to do this would be to load the document into XLinq (which doesn't store prefixes), remove all namespace declaration attributes from it (quite easy) and add namespace declarations on the root element for the namespaces the document needs. And then just save it. The XmlWriter will reuse the declarations from the root element for you.

谢谢,


这篇关于“生成样本XML” - 我想重用名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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