【问题标题】:How do you generate xsd namespace prefixes in the resulting output file?如何在生成的输出文件中生成 xsd 命名空间前缀?
【发布时间】:2026-02-22 13:15:01
【问题描述】:

我已经讨论了多个专门讨论此问题的主题和解决方案。没有一个有效,或者我不明白我做错了什么。

我希望命名空间前缀伴随生成的 xml 输出文件中的元素名称。

我正在通过 VS 命令提示符将 XSD 命令与 Visual Studio 2008 一起使用

这是我的 Trial.xsd

的 xsd 架构
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Junk"
       targetNamespace="http://www.opengis.net/kml/2.2"
       xmlns="http://www.opengis.net/kml/2.2"
       xmlns:gx="http://www.google.com/kml/ext/2.2"
       xmlns:kml="http://www.opengis.net/kml/2.2"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       elementFormDefault="qualified">

<xs:import namespace="http://www.google.com/kml/ext/2.2" schemaLocation="TestSchema.xsd" />

<!-- Start LookType -->
<xs:complexType name="LookType">
  <xs:sequence>
    <xs:element ref="gx:TimeSpan" minOccurs="0" />
    <xs:element name="longitude" type="xs:string" />
  </xs:sequence>
</xs:complexType>
<!-- End LookType -->

<xs:element name="Te" type="LookType" />

</xs:schema>

这是我的 TestSchema.xsd

的 xsd 架构
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="TestSchema"
targetNamespace="http://www.google.com/kml/ext/2.2"
       xmlns="http://www.google.com/kml/ext/2.2"
       xmlns:gx="http://www.google.com/kml/ext/2.2"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       elementFormDefault="qualified">

<xs:element name="TimeSpan" type="gx:GXTimeSpanType" />

<!-- Start GXTimeSpanType -->
<xs:complexType name="GXTimeSpanType">
  <xs:sequence>
    <xs:element name="begin" type="xs:string" />
    <xs:element name="end" type="xs:string" />
  </xs:sequence>
</xs:complexType>
<!-- End GXTimeSpanType -->

</xs:schema>

我正在使用xsd TestSchema.xsd ./Trial.xsd /c /l:cs /n:T 生成生成的类文件Trial.cs

在一个程序中,我分配这些垃圾值并使用XmlDocumentXmlSerializerFileStream 将它们写入文件

T.LookType te = new T.LookType();
te.longitude = "34.444";
te.TimeSpan = new T.GXTimeSpanType();
te.TimeSpan.begin = "2010-02-26T20:22:00Z";
te.TimeSpan.end = "2010-02-26T20:23:42Z";

我已经排除了使用XmlDocumentXmlSerializerFileStream 保存文件的方法,因为它有点厚,然后我想发布,除非这是问题的一部分。

这是我在结果文件中得到的内容

<?xml version="1.0" encoding="utf-16"?>
<Te xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.opengis.net/kml/2.2">

  <TimeSpan xmlns="http://www.google.com/kml/ext/2.2">
    <begin>2010-02-26T20:22:00Z</begin>
    <end>2010-02-26T20:23:42Z</end>
  </TimeSpan>
  <longitude>34.444</longitude>
</Te>

这就是我想要的结果文件。注意gx:TimeSpan 元素和xmlns:gx 命名空间定义被添加到主Te 元素中。

<?xml version="1.0" encoding="utf-16"?>
<Te xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.opengis.net/kml/2.2"
xmlns:gx="http://www.google.com/kml/ext/2.2">
  <gx:TimeSpan>
    <begin>2010-02-26T20:22:00Z</begin>
    <end>2010-02-26T20:23:42Z</end>
  </gx:TimeSpan>
  <longitude>34.444</longitude>
</Te>

【问题讨论】:

    标签: c# visual-studio-2008 xsd


    【解决方案1】:

    前缀在哪个元素上声明并不重要,只要它们在使用前声明即可。任何不喜欢这样的代码都会被破坏。

    也许因为无关紧要,XML Schema 无法影响声明前缀的元素,或使用的前缀。

    【讨论】:

    • 我在某处读到 xmlns="google.com/kml/ext/2.2"> 和 xmlns:gx="google.com/kml/ext/2.2" 在语法上是等价的。然而,只有当 标签出现在 xml 中时,谷歌地球的时间轴滑块才能正常工作。所以我需要在前者上生成它。
    • 第 5.4 节中描述的导入类型 w3.org/TR/xmlschema-0/#import 示例 4 具有我想要的格式,带有限定和非限定元素。想知道为什么我的模式的序列化不会产生这种类型的输出。