在 Visual Studio 2012 中使用两个 xsd 架构 [英] Use two xsd schemas in visual studio 2012

查看:24
本文介绍了在 Visual Studio 2012 中使用两个 xsd 架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成一些 XML 的网络服务.

I've got a webservice that generates some XML.

<Town>
<Countrycode>gb</Countrycode>
<CountryName>United Kingdom</CountryName>
<CleanedAccentCity>Seamill</CleanedAccentCity>
<RegionName>North Ayrshire</RegionName>
<Population>0</Population>
<Distance>0.0497417145329766</Distance>
</Town>

这是我通过添加新项... XML 到架构添加到我的 Visual Studio 2012 项目.

This I added to my Visual Studio 2012 project by Add New Item... XML to schema.

效果很好,我在我的项目中得到了一个 xsd 文件,然后我可以编写代码:

That works great I wind up with an xsd file in my project then in code I can write:

 Public Property returnedXML As XElement

...

  arr = client.DownloadString("http://host/myservice.asmx/GetTopTownsByLatLon?Latitude=" & p.latitude & "&Longitude=" & p.longitude )

     returnedXML = XElement.Parse(arr)

     firstChild = returnedXML.Descendants().First

     City = firstChild...<City>.Value
      etc..

当我在 firstChild 之后输入 .. 时,我得到了智能感知,它显示了 XML 中的属性.

And when I type the .. after firstChild I get intellisense that shows me the attributes in the XML.

现在我需要做的是获得另一个不同的网络服务来为我提供一些其他数据.所以我执行相同的过程,并将我的 XSD 文件保存到项目中.但是,猜猜看,智能感知不再有效.

Now what I need to do is get another different webservice to give me some other data. So I do the same process, and get my XSD files saved into the project. But, guess what, the intellisense no longer works.

显然,我需要确定需要为智能感知读取哪个 XSD 文件.我在哪里设置?

Clearly I need to identify which XSD file needs to be read for the intelisense. Where do I set that?

这是两个 XSD 文件:

Here are the two XSD files:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Towns">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="Town">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="name" type="xs:string" />
              <xs:element name="url" type="xs:string" />
              <xs:element name="Distance" type="xs:unsignedByte" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Towns">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="Town">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Countrycode" type="xs:string" />
              <xs:element name="CountryName" type="xs:string" />
              <xs:element name="CleanedAccentCity" type="xs:string" />
              <xs:element name="RegionName" type="xs:string" />
              <xs:element name="Population" type="xs:unsignedByte" />
              <xs:element name="Distance" type="xs:decimal" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

谢谢

推荐答案

如果这些方案定义之间存在冲突(如重复的元素定义等),则源代码未编译且智能感知不起作用.

If there are conflicts between these schemes definitions (like duplicated element definitions etc.), the source is not compiled and intellisense not work.

你能共享两个 XSD 文件吗?

Can you share both XSD files?

两个 XSD 都有重复的元素()并且没有命名空间定义,因此它们之间没有区别.为了正常工作,您有两种方法:

Both XSD has a duplicate elements (<Towns>, <Town> and <Distance>) and no namespace definitions, so there is no way to difference between theirs. In order to works properly you have two ways:

  • 第一种方法:仅在您的一个 XSD 中更改这些名称元素,例如 :

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"    xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="TownsA">
<xs:complexType>
  <xs:sequence>
    <xs:element maxOccurs="unbounded" name="TownA">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="name" type="xs:string" />
          <xs:element name="url" type="xs:string" />
          <xs:element name="DistanceA" type="xs:unsignedByte" />
        </xs:sequence>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType>

并保持第二个不变:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Towns">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="Town">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Countrycode" type="xs:string" />
              <xs:element name="CountryName" type="xs:string" />
              <xs:element name="CleanedAccentCity" type="xs:string" />
              <xs:element name="RegionName" type="xs:string" />
              <xs:element name="Population" type="xs:unsignedByte" />
              <xs:element name="Distance" type="xs:decimal" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

  • 第二种方式:在 :
  • 中的架构中添加不同的 targetNamespaceNamespaces

    • Second way: Add a different targetNamespaceNamespaces in your schemas in <xs:schema>:
    • 第一个 XSD:

      <xs:schema 
             attributeFormDefault="unqualified" 
             elementFormDefault="qualified" 
             targetNamespace="http://mynamespace.org/1"
             xmlns:xs="http://www.w3.org/2001/XMLSchema">
      

      第二个 XSD:

      <xs:schema 
             attributeFormDefault="unqualified" 
             elementFormDefault="qualified" 
             targetNamespace="http://mynamespace.org/2"
             xmlns:xs="http://www.w3.org/2001/XMLSchema">
      

      这篇关于在 Visual Studio 2012 中使用两个 xsd 架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆