您应该映射每个导入的模式:
<?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">
elementForm默认值仅适用于它所在的架构,并且不会覆盖任何包含或导入中的设置。
如果您想隐藏命名空间,那么所有模式都必须指定元素表单默认值=“不合格的".同样,如果您想公开每个模式必须指定的名称空间元素表单默认值=“合格的"
UPDATED查看单元测试后:
您的输入:
<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>
你的输出:
<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>
突出的问题是标签的重复 - 在我看来一切都很好,但仍在努力理解为什么会发生这种情况。
周一更新:
我认为 LinqToXSD 工具中有一个错误 - 我已经运行了我能想到的所有组合,并且无法始终如一地解决您的问题,但是......我已经设法修复了<Tmats>
重复问题:
在 XmlHelper 文件中,更改 return 语句:
System.Xml.Linq.XDocument xd = System.Xml.Linq.XDocument.Parse(sb.ToString());
return xd.Root.FirstNode.ToString();
我知道这是一个hack,但它解决了问题并且您的 LoopbackTest 通过了。
如果使用 Tmats 类创建元素,则不会获得任何前缀,我已经尝试了各种属性组合,我能做的最好的事情就是重新附加命名空间。如果您正在与外部系统交换信息,那么我确实有一个fix:
- 在代码中使用 Tmats 对象,
- 使用命名空间将其序列化,
- 通过 XSLT 运行它以将 ns 映射到前缀。
我知道它很笨重,但我认为这是您在实际修复 LinqToXSD 代码时所能得到的最好的结果。
XSLT 将命名空间映射到前缀(您需要在“样式表”声明和“映射器”中维护命名空间集:
<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>
好的,所以这远非理想,但是只有当您需要与需要修复 xml 输出的其他人交互时,您的代码才能在项目内部正常工作(请记住更改elementFormDefault="合格"(或将其删除)在您的 XSD 中) - 如果您将 XSLT 缓存为XslCompiledTransform
你几乎不会注意到它的发生。