C# 如何让LinqToXSD正确输出名称空间前缀声明?
我正在尝试使用XML数据绑定类和包含大量导入模式的XML模式创建XML数据绑定类。所有模式都是 为此,我使用了以下根模式文档:C# 如何让LinqToXSD正确输出名称空间前缀声明?,c#,.net-3.5,xml-serialization,xsd,linq-to-xsd,C#,.net 3.5,Xml Serialization,Xsd,Linq To Xsd,我正在尝试使用XML数据绑定类和包含大量导入模式的XML模式创建XML数据绑定类。所有模式都是 为此,我使用了以下根模式文档: <?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:TmatsG="http://www.spiraltechinc.com/tmats/106-13/TmatsG" elementFormDefault=
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:TmatsG="http://www.spiraltechinc.com/tmats/106-13/TmatsG" elementFormDefault="unqualified">
<xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon" schemaLocation="TmatsCommonTypes.xsd"/>
<xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsC" schemaLocation="TmatsCGroup.xsd"/>
<xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsD" schemaLocation="TmatsDGroup.xsd"/>
<xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsG" schemaLocation="TmatsGGroup.xsd"/>
<xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsH" schemaLocation="TmatsHGroup.xsd"/>
<xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsM" schemaLocation="TmatsMGroup.xsd"/>
<xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsP" schemaLocation="TmatsPGroup.xsd"/>
<xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsR" schemaLocation="TmatsRGroup.xsd"/>
<xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsS" schemaLocation="TmatsSGroup.xsd"/>
<xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsT" schemaLocation="TmatsTGroup.xsd"/>
<xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsV" schemaLocation="TmatsVGroup.xsd"/>
<xs:element name="Tmats" type="TmatsG:Tmats">
<xs:annotation>
<xs:documentation>Tmats Root</xs:documentation>
</xs:annotation>
</xs:element>
</xs:schema>
我期望输出如下所示:
<Tmats>
<TmatsG:ProgramName>My Program</TmatsG:ProgramName>
<TmatsG:OriginationDate>2012-05-09-07:00</TmatsG:OriginationDate>
<TmatsG:PointOfContact>
<TmatsCommon:Name>Robert Harvey</TmatsCommon:Name>
<TmatsCommon:Agency>My Agency</TmatsCommon:Agency>
<TmatsCommon:Address>12345 Anywhere Street</TmatsCommon:Address>
<TmatsCommon:Telephone>111-222-3333</TmatsCommon:Telephone>
</TmatsG:PointOfContact>
</Tmats>
我的节目
2012-05-09-07:00
罗伯特·哈维
我的代理
Anywhere街12345号
111-222-3333
相反,我得到的是:
<Tmats>
<ProgramName xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsG">My Program</ProgramName>
<OriginationDate xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsG">2012-05-09-07:00</OriginationDate>
<PointOfContact xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsG">
<Name xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">Robert Harvey</Name>
<Agency xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">My Agency</Agency>
<Address xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">12345 Anywhere Street</Address>
<Telephone xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">111-222-3333</Telephone>
</PointOfContact>
</Tmats>
我的节目
2012-05-09-07:00
罗伯特·哈维
我的代理
Anywhere街12345号
111-222-3333
有没有办法让LinqToXSD产生预期的输出 您应该映射每个导入的架构:
<?xml version="1.0"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:TmatsCommon="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon"
xmlns:TmatsC="http://www.spiraltechinc.com/tmats/106-13/TmatsC"
xmlns:TmatsD="http://www.spiraltechinc.com/tmats/106-13/TmatsD"
xmlns:TmatsG="http://www.spiraltechinc.com/tmats/106-13/TmatsG"
xmlns:TmatsH="http://www.spiraltechinc.com/tmats/106-13/TmatsH"
xmlns:TmatsM="http://www.spiraltechinc.com/tmats/106-13/TmatsM"
…
elementFormDefault="unqualified">
我知道这是一个黑客,但它解决了问题,并且你的环回测试通过了
如果使用Tmats类创建元素,就不会得到任何前缀,我已经尝试了各种属性组合,我所能做的就是重新附加名称空间。如果您正在与外部系统交换信息,则我确实有一个修复程序:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mapper="http://mapper"
xmlns:TmatsCommon="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon"
xmlns:TmatsC="http://www.spiraltechinc.com/tmats/106-13/TmatsC"
xmlns:TmatsD="http://www.spiraltechinc.com/tmats/106-13/TmatsD"
xmlns:TmatsG="http://www.spiraltechinc.com/tmats/106-13/TmatsG"
xmlns:TmatsH="http://www.spiraltechinc.com/tmats/106-13/TmatsH"
xmlns:TmatsM="http://www.spiraltechinc.com/tmats/106-13/TmatsM"
xmlns:TmatsP="http://www.spiraltechinc.com/tmats/106-13/TmatsP"
xmlns:TmatsR="http://www.spiraltechinc.com/tmats/106-13/TmatsR"
xmlns:TmatsS="http://www.spiraltechinc.com/tmats/106-13/TmatsS"
xmlns:TmatsT="http://www.spiraltechinc.com/tmats/106-13/TmatsT"
xmlns:TmatsV="http://www.spiraltechinc.com/tmats/106-13/TmatsV">
<xsl:output omit-xml-declaration="no" indent="yes"/>
<xsl:strip-space elements="*"/>
<mapper:namespaces>
<ns prefix="TmatsCommon" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon"/>
<ns prefix="TmatsC" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsC"/>
<ns prefix="TmatsD" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsD"/>
<ns prefix="TmatsG" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsG"/>
<ns prefix="TmatsH" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsH"/>
<ns prefix="TmatsM" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsM"/>
<ns prefix="TmatsP" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsP"/>
<ns prefix="TmatsR" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsR"/>
<ns prefix="TmatsS" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsS"/>
<ns prefix="TmatsT" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsT"/>
<ns prefix="TmatsV" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsV"/>
</mapper:namespaces>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[namespace-uri()=document('')/*/mapper:namespaces/*/@uri]">
<xsl:variable name="vNS" select="document('')/*/mapper:namespaces/*[@uri=namespace-uri(current())]"/>
<xsl:element name="{$vNS/@prefix}:{local-name()}" namespace="{namespace-uri()}" >
<xsl:copy-of select="namespace::*[not(. = namespace-uri(current()))]"/>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
产生:
<Tmats>
<TmatsG:ProgramName xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsG">My Program</TmatsG:ProgramName>
<TmatsG:OriginationDate xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsG">2012-05-09-07:00</TmatsG:OriginationDate>
<TmatsG:PointOfContact xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsG">
<TmatsCommon:Name xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">Robert Harvey</TmatsCommon:Name>
<TmatsCommon:Agency xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">My Agency</TmatsCommon:Agency>
<TmatsCommon:Address xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">12345 Anywhere Street</TmatsCommon:Address>
<TmatsCommon:Telephone xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">111-222-3333</TmatsCommon:Telephone>
</TmatsG:PointOfContact>
</Tmats>
我的节目
2012-05-09-07:00
罗伯特·哈维
我的代理
Anywhere街12345号
111-222-3333
好的,这远非理想,但您的代码在项目内部运行良好,只有当您需要与其他人交互时,您才需要修复xml输出(请记住在XSD中更改elementFormDefault=“qualified”(或删除它)-如果将XSLT缓存为
XslCompiledTransform
,您几乎不会注意到它的发生。我尝试了这一点,它实际上修复了LinqToXSD生成的古怪的底层C#名称空间(名称空间www.spiratechinc.com.tmats.item106.item13.TmatsC
变成了TmatsC
),但保存的XML输出没有改变;它仍然在每个元素中包含xmlns声明。我认为这与此有关,但我不知道如何将其合并到LinqToXSD类中。我必须马上开始工作-如果你可以打包项目并上传到某个地方,我很高兴今晚晚些时候为你玩它-这是一个有趣的问题。项目在这里:。注意:使用上面写着单击此处的链接从sendspace开始下载
。忽略其他虚假的下载按钮和链接…嘘。看看测试项目中的单元测试。很抱歉,我无法为您解决此问题-唯一的方法是在实际的源代码中进行黑客攻击。谢谢-如果您确信模式不太可能更改,那么可以对Tmats.cs
文件中的所有元素进行注释,以帮助序列化程序,但就个人而言,我只需要使用样式表对其进行转换,就可以了。
<Tmats>
<Tmats
xmlns:TmatsG="http://www.spiraltechinc.com/tmats/106-13/TmatsG"
xmlns:TmatsCommon="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">
<TmatsG:ProgramName>My Project</TmatsG:ProgramName>
<TmatsG:OriginationDate>2012-05-15</TmatsG:OriginationDate>
System.Xml.Linq.XDocument xd = System.Xml.Linq.XDocument.Parse(sb.ToString());
return xd.Root.FirstNode.ToString();
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mapper="http://mapper"
xmlns:TmatsCommon="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon"
xmlns:TmatsC="http://www.spiraltechinc.com/tmats/106-13/TmatsC"
xmlns:TmatsD="http://www.spiraltechinc.com/tmats/106-13/TmatsD"
xmlns:TmatsG="http://www.spiraltechinc.com/tmats/106-13/TmatsG"
xmlns:TmatsH="http://www.spiraltechinc.com/tmats/106-13/TmatsH"
xmlns:TmatsM="http://www.spiraltechinc.com/tmats/106-13/TmatsM"
xmlns:TmatsP="http://www.spiraltechinc.com/tmats/106-13/TmatsP"
xmlns:TmatsR="http://www.spiraltechinc.com/tmats/106-13/TmatsR"
xmlns:TmatsS="http://www.spiraltechinc.com/tmats/106-13/TmatsS"
xmlns:TmatsT="http://www.spiraltechinc.com/tmats/106-13/TmatsT"
xmlns:TmatsV="http://www.spiraltechinc.com/tmats/106-13/TmatsV">
<xsl:output omit-xml-declaration="no" indent="yes"/>
<xsl:strip-space elements="*"/>
<mapper:namespaces>
<ns prefix="TmatsCommon" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon"/>
<ns prefix="TmatsC" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsC"/>
<ns prefix="TmatsD" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsD"/>
<ns prefix="TmatsG" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsG"/>
<ns prefix="TmatsH" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsH"/>
<ns prefix="TmatsM" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsM"/>
<ns prefix="TmatsP" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsP"/>
<ns prefix="TmatsR" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsR"/>
<ns prefix="TmatsS" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsS"/>
<ns prefix="TmatsT" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsT"/>
<ns prefix="TmatsV" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsV"/>
</mapper:namespaces>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[namespace-uri()=document('')/*/mapper:namespaces/*/@uri]">
<xsl:variable name="vNS" select="document('')/*/mapper:namespaces/*[@uri=namespace-uri(current())]"/>
<xsl:element name="{$vNS/@prefix}:{local-name()}" namespace="{namespace-uri()}" >
<xsl:copy-of select="namespace::*[not(. = namespace-uri(current()))]"/>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
<Tmats>
<TmatsG:ProgramName xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsG">My Program</TmatsG:ProgramName>
<TmatsG:OriginationDate xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsG">2012-05-09-07:00</TmatsG:OriginationDate>
<TmatsG:PointOfContact xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsG">
<TmatsCommon:Name xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">Robert Harvey</TmatsCommon:Name>
<TmatsCommon:Agency xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">My Agency</TmatsCommon:Agency>
<TmatsCommon:Address xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">12345 Anywhere Street</TmatsCommon:Address>
<TmatsCommon:Telephone xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">111-222-3333</TmatsCommon:Telephone>
</TmatsG:PointOfContact>
</Tmats>