繁体   English   中英

在xsd.exe生成的类之后设置适当的XML名称空间

[英]Set proper XML namespaces after classes generated by xsd.exe

我使用xsd.exe工具从xsd生成了c#类。 在原始架构中,名称空间如下:

<xs:schema xmlns="http://TheXsdGivenSchema.com/SOMETHING/1.0/stuff" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://TheXsdGivenSchema.com/SOMETHING/1.0/stuff" elementFormDefault="qualified" attributeFormDefault="unqualified">

生成的类的根如下:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]    
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://TheXsdGivenSchema.com/SOMETHING/1.0/stuff")]    
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://TheXsdGivenSchema.com/SOMETHING/1.0/stuff", IsNullable = false)]          

我的问题是,在我对此类进行序列化之后,我得到了具有以下架构的XML:

<someThing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://TheXsdGivenSchema.com/SOMETHING/1.0/stuff">

但是我需要的是:

<tns:someThing xmlns:tns="http://TheXsdGivenSchema.com/SOMETHING/1.0/stuff" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://TheXsdGivenSchema.com/SOMETHING/1.0/stuff">

为了使事情变得更加有趣,我需要在根目录和其他一些节点中使用tns:前缀。 像这样:

<tns:someThing xmlns:tns="http://TheXsdGivenSchema.com/SOMETHING/1.0/stuff" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://TheXsdGivenSchema.com/SOMETHING/1.0/stuff">
<tns:header>
    <tns:request>id-707</tns:request>
    <tns:timestamp>2015-01-29T12:18:29+01:00</tns:timestamp>
</tns:header>
<tns:user>
    <tns:user>myUserName</tns:user> 
    <tns:password>hashedPwd</tns:password>
</tns:user>
<someOperations>
    <someOperation>
         <id>1212</id>
         <name>nameOfThis</name>
    </someOperation>
</someOperations>
</tns:someThing>

如何正确设置这些名称空间和前缀? 提前致谢!

此方法应有助于命名空间前缀:

var namespaces = new XmlSerializerNamespaces();
namespaces.Add("tns", "http://TheXsdGivenSchema.com/SOMETHING/1.0/stuff");
var serializer = new XmlSerializer(typeof(entity));
serializer.Serialize( stream, entity, namespaces);

从以下答案中获取: https : //social.msdn.microsoft.com/Forums/vstudio/en-US/2ed55114-0f1b-4e54-b597-21fe1a61f06b/add-prefixes-and-namesapce-to-xml-serialization?forum =一般

暂无
暂无

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

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