C#生成XMLSchema文档

超人不会敲代码 2017-03-01 05:07:26
我需要在C#中把实体类生成XML文件,我已经生成了,但是节点是这样的:<sequence>,我需要生成<xs:sequence>这样的前缀的。
不知如何解决呢,有大神吗?
...全文
189 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
assange_h 2017-03-02
  • 打赏
  • 举报
回复
如果你用的是DataSet这个可以使用他自己的 .toXml 方法 XmlWriteMode有多个属性可以按照你的要求生成相应的格式 DataSet.WriteXml("Customers.xml", XmlWriteMode.WriteSchema);
xdashewan 2017-03-02
  • 打赏
  • 举报
回复
引用 5 楼 zz294487918 的回复:
前面没有 xs:
我不知道你代码怎么写的,例子我运行了一下,只字未改,下面是结果

<?xml version="1.0" encoding="utf-8"?>
<Books xmlns:books="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com">
  <money:Book>
    <books:TITLE>A Book Title</books:TITLE>
    <money:PRICE books:currency="US Dollar">
      <money:price>9.95</money:price>
    </money:PRICE>
  </money:Book>
</Books>
  • 打赏
  • 举报
回复
引用 4 楼 xdashewan 的回复:
[quote=引用 2 楼 zz294487918 的回复:] 谢谢啦 XmlSerializerNamespaces具体怎么用呀 我用的是XmlDocument
msdn上有具体例子,就是我给的url[/quote] 上面的例子我试过了 不对啊 我想要这样的: <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="note"> <xs:complexType> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> 可是我自己生成的是: <?xml version="1.0"?> <schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <element Name="DTO_QRcode"> <complexType> <sequence> <element Name="code" Type="xs:string" /> <element Name="QRcode" Type="xs:string" /> </sequence> </complexType> </element> </schema> 前面没有 xs:
xdashewan 2017-03-02
  • 打赏
  • 举报
回复
引用 2 楼 zz294487918 的回复:
谢谢啦 XmlSerializerNamespaces具体怎么用呀 我用的是XmlDocument
msdn上有具体例子,就是我给的url
  • 打赏
  • 举报
回复
引用 1 楼 xdashewan 的回复:
用XmlSerializerNamespaces类,参照:https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializernamespaces.aspx
谢谢啦 XmlSerializerNamespaces具体怎么用呀 我用的是XmlDocument
  • 打赏
  • 举报
回复
谢谢啦 XmlSerializerNamespaces具体怎么用呀 我用的是XmlDocument
  • 打赏
  • 举报
回复
用XmlSchema 类已经解决。
<?xml version="1.0" encoding="IBM437"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="cat" type="xs:string"/>
    <xs:element name="dog" type="xs:string"/>
    <xs:element name="redDog" type="xs:string" substitutionGroup="dog"/>
    <xs:element name="brownDog" type="xs:string" substitutionGroup ="dog" />

    <xs:element name="pets">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element ref="cat"/>
          <xs:element ref="dog"/>
        </xs:choice>
      </xs:complexType>
    </xs:element>
</xs:schema>
参考 https://msdn.microsoft.com/en-us/library/system.xml.schema.xmlschema(v=vs.110).aspx
  • 打赏
  • 举报
回复
引用 6 楼 xdashewan 的回复:
[quote=引用 5 楼 zz294487918 的回复:] 前面没有 xs:
我不知道你代码怎么写的,例子我运行了一下,只字未改,下面是结果

<?xml version="1.0" encoding="utf-8"?>
<Books xmlns:books="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com">
  <money:Book>
    <books:TITLE>A Book Title</books:TITLE>
    <money:PRICE books:currency="US Dollar">
      <money:price>9.95</money:price>
    </money:PRICE>
  </money:Book>
</Books>
[/quote]
引用 6 楼 xdashewan 的回复:
[quote=引用 5 楼 zz294487918 的回复:] 前面没有 xs:
我不知道你代码怎么写的,例子我运行了一下,只字未改,下面是结果

<?xml version="1.0" encoding="utf-8"?>
<Books xmlns:books="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com">
  <money:Book>
    <books:TITLE>A Book Title</books:TITLE>
    <money:PRICE books:currency="US Dollar">
      <money:price>9.95</money:price>
    </money:PRICE>
  </money:Book>
</Books>
[/quote] 这个我也运行了,不是我的需求啊, 我用XmlSchema 类已经解决了,还是感谢你哈
xdashewan 2017-03-01
  • 打赏
  • 举报
回复
用XmlSerializerNamespaces类,参照:https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializernamespaces.aspx

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧