【问题标题】:How to create XSD for the given XML?如何为给定的 XML 创建 XSD?
【发布时间】:2014-04-15 11:28:39
【问题描述】:

对于下面的 XML,我需要生成 XSD,但出现错误

'NewDataSet' 元素未声明

<NewDataSet>
 <Table>
  <SITE>VMD</SITE>
  <TANK>65-12-392</TANK>
  <SERVICE>HZLPG</SERVICE>
  <IP21TAG>BC-BBH-OS-4LI21392</IP21TAG>
 </Table>
</NewDataSet>

XSD:

<?xml version="1.0"?>
 <xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="PAS">
 <xs:complexType>
   <xs:sequence>
<xs:element name="Records">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="Site" type="xs:string" />
      <xs:element name="Plant" type="xs:string" />
      <xs:element name="Tank" type="xs:string" />
      <xs:element name="Service" type="xs:string" />
      <xs:element name="IP21Tag" type="xs:string" />
    </xs:sequence>
   </xs:complexType>
  </xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

谁能帮助如何声明'NewDataSet'元素? 提前致谢。

【问题讨论】:

  • xsd.exe 中的错误是什么,或者当您使用 xsd.exe 生成的 c# 时出现什么错误?
  • 使用 C# 验证 XML 和 XSD 时出现错误,例如未声明“NewDataSet”元素
  • 你能显示你正在使用的代码吗?
  • 您的 XML 与您的 XSD 不匹配 - 甚至没有一个元素匹配(请记住 XML 区分大小写,因此 &lt;Tank&gt;&lt;TANK&gt; 不同

标签: c# xml xsd xsd-validation


【解决方案1】:

您可以使用 Microsoft 提供的 xsd.exe 从示例 XML 生成 XSD。您将只生成一个与您的示例匹配的 XSD,因此您需要一个示例来包含您希望解析的所有案例,或者您必须进一步编辑 XSD 以包含这些案例。

无论如何,这是一个很好的入门方式。 MSDN Docs on XSD.exe

一旦您有了 XSD 文件,您可能想要使用像 XSD2Code 这样的工具,它会生成读取 XML 所需的所有代码,并将其转换为内存中的一组 c# 对象。

(假设您可以一次将所有 XML 读入内存。否则,您将需要使用事件 SAX 类型的方法来读取 XML。)

【讨论】:

    【解决方案2】:

    首先,根元素“NewData”并未在您的 XSD 中声明。 我建议你先看看这个:http://www.w3schools.com/schema/schema_example.asp

    【讨论】:

      【解决方案3】:

      XmlSchemaInference 类可用于将 xml 转换为 xsd,例如:

              XmlReader reader = XmlReader.Create ( "contosoBooks.xml" );  
              XmlSchemaSet schemaSet = new XmlSchemaSet ( );  
              XmlSchemaInference schema = new XmlSchemaInference ( );  
              schemaschemaSet = schema.InferSchema ( reader );  
              foreach ( XmlSchema s in schemaSet.Schemas ( ) )  
              {  
                  s.Write ( Console.Out );  
              } 
      

      http://msdn.microsoft.com/en-us/library/system.xml.schema.xmlschemainference.aspx

      第二种方式,

      示例如下:

      Process p = new Process(); 
      p.StartInfo.UseShellExecute = false; 
      p.StartInfo.RedirectStandardOutput = true; 
      p.StartInfo.FileName = "Xsd.exe"; 
      p.StartInfo.Arguments = "C:\\config.xml /outputdir:C:\\Temp"; 
      p.Start(); 
      string output = p.StandardOutput.ReadToEnd(); 
      p.WaitForExit(); 
      Console.WriteLine("OUTPUT FROM XSD.EXE : " + output); 
      

      这将从 config.xml 创建 config.xsd 文件。

      所有功劳归于在msdn 论坛上回答的人。

      【讨论】:

      • 我不明白你为什么要编写代码来调用xsd.exe?您可以在控制台上调用它。他没有说他必须在运行时这样做。
      • @AnthonyLambert,是的,在这种情况下XSD.exe就足够了。为此 +1。
      猜你喜欢
      • 2015-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-31
      • 1970-01-01
      • 2022-01-20
      相关资源
      最近更新 更多