开发者

Is there a tool to generate simple c# classes from an xsd?

开发者 https://www.devze.com 2023-03-05 17:17 出处:网络
I have an xsd (greatly simplified for this post): <?xml version=\"1.0\" encoding=\"utf-8\"?> <xs:schema id=\"serviceResponse\" xmlns=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns

I have an xsd (greatly simplified for this post):

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="serviceResponse" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="serviceResponse" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="searchResults">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="numberOfResults" type="xs:string" minOccurs="0" />
              <xs:element name="raceList" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="race" minOccurs="0" maxOccurs="unbounded">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="id" type="xs:string" minOccurs="0" />
                          <xs:element name="title" type="xs:string" minOccurs="0" />
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
          开发者_开发百科  </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

I need simple c# classes:

public class serviceResponse
{
    public searchResults SearchResults { get; set; }
}

public class searchResults
{
    public string numberOfResults { get; set; }
    public List<race> raceList { get; set; }
}

public class race
{
    public string id { get; set; }
    public string title { get; set; }   
}

I was told xsd.exe or even csxsd.exe can do this, but don't see the flags that just get simple usable classes out of those. I just need super simple classes that can be populated with xml from restful api calls. I do not want to hand code 150+ differenty object types. Is there a tool that does this?


Try Xsd2Code - free and available from CodePlex. Visual Studio Plugin to right-click on XSD and generate .cs file for it.

Update (May 2018):
Unfortunately, this tool is no longer available for free. You can find the new Xsd2Code website here and see the licensing options.

Is there a tool to generate simple c# classes from an xsd?


You can use xsd.exe for this.

What exactly makes this unsuitable?


You also can include xsd in your Visual Studio Extwermal tools like that: In VS:

1.Select TOOLS

2.Select EXTERNAL TOOLS

3.Select ADD

◦Title = give a meaningful title, like "XSD"

◦Command = C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\xsd.exe this is the default install location

◦Arguments = $(ItemPath) /c /l:vb use the standard XSD switches you normally would. In the above "/c /l:vb" means create a VB class out of the current XSD document

◦Item Directory = your choice, but if you want the output in the same directory/folder as the xsd file, $(ItemDir)

◦follow the advice above to check "Use Output Window" so you can see what's going on

4.Click APPLY After performing the above, you'll see "XSD" (or whatever Title you entered) the next time your select TOOLS. Select it to run xsd.exe on the current document.

0

精彩评论

暂无评论...
验证码 换一张
取 消