如何让LinqToXSD正确输出名称空间前缀声明?

Rob*_*vey 8 c# xsd xml-serialization linq-to-xsd .net-3.5

我正在尝试使用LinqToXSD创建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="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>
Run Code Online (Sandbox Code Playgroud)

我使用Linq到XSD创建了类.然后我写了以下测试:

[TestMethod()]
public void TmatsXmlExample4()
{
    Tmats tmats = new Tmats
    {
        ProgramName = "My Program",
        OriginationDate = DateTime.Now,
    };
    tmats.PointOfContact.Add(new PointOfContactType
    {
         Address = "12345 Anywhere Street",
         Agency = "My Agency",
         Name = "Robert Harvey",
         Telephone = "111-222-3333"
    });
    Debug.Print(tmats.ToString());
}
Run Code Online (Sandbox Code Playgroud)

我期望输出看起来像这样:

<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>
Run Code Online (Sandbox Code Playgroud)

相反,我得到的是这样的:

<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>
Run Code Online (Sandbox Code Playgroud)

有没有办法让LinqToXSD产生预期的输出?

web*_*bod 1

您应该映射每个导入的模式:

\n\n
<?xml version="1.0"?>\n    <xs:schema \n        xmlns:xs="http://www.w3.org/2001/XMLSchema"\n        xmlns:TmatsCommon="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon"\n        xmlns:TmatsC="http://www.spiraltechinc.com/tmats/106-13/TmatsC"\n        xmlns:TmatsD="http://www.spiraltechinc.com/tmats/106-13/TmatsD"\n        xmlns:TmatsG="http://www.spiraltechinc.com/tmats/106-13/TmatsG"\n        xmlns:TmatsH="http://www.spiraltechinc.com/tmats/106-13/TmatsH"\n        xmlns:TmatsM="http://www.spiraltechinc.com/tmats/106-13/TmatsM"\n        \xe2\x80\xa6 \n        elementFormDefault="unqualified">\n
Run Code Online (Sandbox Code Playgroud)\n\n

elementFormDefault仅适用于它所在的架构,并且不会覆盖任何包含或导入中的设置。

\n\n

如果要隐藏命名空间,则所有架构都必须指定elementFormDefault=" unqualified "。同样,如果您想公开名称空间,每个架构都必须指定elementFormDefault=“ qualified

\n\n

检查单元测试后更新:

\n\n

您的输入:

\n\n
<Tmats \n    xmlns:TmatsG="http://www.spiraltechinc.com/tmats/106-13/TmatsG"\n    xmlns:TmatsCommon="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">\n    <TmatsG:ProgramName>My Project</TmatsG:ProgramName> \n    <TmatsG:OriginationDate>2012-05-15</TmatsG:OriginationDate> \n
Run Code Online (Sandbox Code Playgroud)\n\n

你的输出:

\n\n
<Tmats> \n    <Tmats \n        xmlns:TmatsG="http://www.spiraltechinc.com/tmats/106-13/TmatsG"\n        xmlns:TmatsCommon="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">\n        <TmatsG:ProgramName>My Project</TmatsG:ProgramName> \n        <TmatsG:OriginationDate>2012-05-15</TmatsG:OriginationDate> \n
Run Code Online (Sandbox Code Playgroud)\n\n

突出的问题是标签的重复 - 在我看来一切都很好,但仍在努力理解为什么会发生这种情况。

\n\n

周一更新:

\n\n

我认为 LinqToXSD 工具中有一个错误 - 我已经运行了我能想到的所有组合,但无法始终如一地解决您的问题,但是......我已经设法修复了<Tmats>

\n\n

在 XmlHelper 文件中,更改 return 语句:

\n\n
System.Xml.Linq.XDocument xd = System.Xml.Linq.XDocument.Parse(sb.ToString());\nreturn xd.Root.FirstNode.ToString();\n
Run Code Online (Sandbox Code Playgroud)\n\n

我知道这是一个hack,但它解决了问题并且您的 LoopbackTest 通过了。

\n\n

如果使用 Tmats 类创建元素,则不会获得任何前缀,我已经尝试了各种属性组合,我能做的最好的事情就是重新附加命名空间。如果您正在与外部系统交换信息,那么我确实有一个修复方法

\n\n
    \n
  1. 在代码中使用 Tmats 对象,
  2. \n
  3. 使用命名空间将其序列化,
  4. \n
  5. 通过 XSLT 运行它以将 ns 映射到前缀。
  6. \n
\n\n

我知道它很笨重,但我认为这是您在实际修复 LinqToXSD 代码时所遇到的最好的方法。

\n\n

XSLT 将命名空间映射到前缀(您需要在“样式表”声明和“映射器”中维护命名空间集:

\n\n
<xsl:stylesheet version="2.0"\n    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"\n    xmlns:mapper="http://mapper"\n    xmlns:TmatsCommon="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon"\n    xmlns:TmatsC="http://www.spiraltechinc.com/tmats/106-13/TmatsC"\n    xmlns:TmatsD="http://www.spiraltechinc.com/tmats/106-13/TmatsD"\n    xmlns:TmatsG="http://www.spiraltechinc.com/tmats/106-13/TmatsG"\n    xmlns:TmatsH="http://www.spiraltechinc.com/tmats/106-13/TmatsH"\n    xmlns:TmatsM="http://www.spiraltechinc.com/tmats/106-13/TmatsM"\n    xmlns:TmatsP="http://www.spiraltechinc.com/tmats/106-13/TmatsP"\n    xmlns:TmatsR="http://www.spiraltechinc.com/tmats/106-13/TmatsR"\n    xmlns:TmatsS="http://www.spiraltechinc.com/tmats/106-13/TmatsS"\n    xmlns:TmatsT="http://www.spiraltechinc.com/tmats/106-13/TmatsT"\n    xmlns:TmatsV="http://www.spiraltechinc.com/tmats/106-13/TmatsV">\n  <xsl:output omit-xml-declaration="no" indent="yes"/>\n  <xsl:strip-space elements="*"/>\n  <mapper:namespaces>\n    <ns prefix="TmatsCommon" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon"/>\n    <ns prefix="TmatsC" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsC"/>\n    <ns prefix="TmatsD" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsD"/>\n    <ns prefix="TmatsG" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsG"/>\n    <ns prefix="TmatsH" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsH"/>\n    <ns prefix="TmatsM" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsM"/>\n    <ns prefix="TmatsP" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsP"/>\n    <ns prefix="TmatsR" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsR"/>\n    <ns prefix="TmatsS" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsS"/>\n    <ns prefix="TmatsT" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsT"/>\n    <ns prefix="TmatsV" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsV"/>\n  </mapper:namespaces>\n  <xsl:template match="node()|@*">\n     <xsl:copy>\n       <xsl:apply-templates select="node()|@*"/>\n     </xsl:copy>\n </xsl:template>\n\n <xsl:template match="*[namespace-uri()=document(\'\')/*/mapper:namespaces/*/@uri]">\n  <xsl:variable name="vNS" select="document(\'\')/*/mapper:namespaces/*[@uri=namespace-uri(current())]"/>\n  <xsl:element name="{$vNS/@prefix}:{local-name()}" namespace="{namespace-uri()}" >\n   <xsl:copy-of select="namespace::*[not(. = namespace-uri(current()))]"/>\n   <xsl:copy-of select="@*"/>\n   <xsl:apply-templates/>\n  </xsl:element>\n </xsl:template>\n</xsl:stylesheet>\n
Run Code Online (Sandbox Code Playgroud)\n\n

生产:

\n\n
<Tmats>\n  <TmatsG:ProgramName xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsG">My Program</TmatsG:ProgramName>\n  <TmatsG:OriginationDate xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsG">2012-05-09-07:00</TmatsG:OriginationDate>\n  <TmatsG:PointOfContact xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsG">\n    <TmatsCommon:Name xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">Robert Harvey</TmatsCommon:Name>\n    <TmatsCommon:Agency xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">My Agency</TmatsCommon:Agency>\n    <TmatsCommon:Address xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">12345 Anywhere Street</TmatsCommon:Address>\n    <TmatsCommon:Telephone xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">111-222-3333</TmatsCommon:Telephone>\n  </TmatsG:PointOfContact>\n</Tmats>\n
Run Code Online (Sandbox Code Playgroud)\n\n

好的,所以这远非理想,但是只有当您需要与需要修复 xml 输出的其他人交互时,您的代码才能在项目内部正常工作(请记住更改 elementFormDefault=" qualified"(或删除它)) - 如果您将 XSLT 缓存为 a,XslCompiledTransform您几乎不会注意到它发生。

\n