xsd에서 .NET 4.0 클래스를 생성하는 방법은 무엇입니까?
Visual Studio 2010을 사용하여 xsd 파일에서 .NET 4.0 c# 클래스(엔티티)를 생성하는 옵션은 무엇입니까?
간단하게 실행할 수 있습니다(vs 명령 프롬프트에서).
xsd your.xsd /classes
는 (을) 만들 입니다.your.cs인 옵션들은 대부분 0 만여 기포에 2.0 하크않 2.0 바습다 2.0 았니 뀌지게 2.0 이후로 2.0 지은옵 2.0 션기 의 2.분▁here▁2▁). ▁much다니▁'▁▁haven▁oft습▁note▁most▁the▁options,았▁intrinsic,바▁that뀌▁however지 2.
옵션의 경우 다음을 사용합니다.xsd /?또는 MSDN을 참조하십시오(예:/enableDataBinding유용할 수 있습니다.
xsd.exe는 Mark Gravell이 언급한 것과 같습니다.IMO를 시작하고 실행하는 가장 빠른 방법.
또는 더 많은 유연성/옵션이 필요한 경우:
xsd2 코드 VS 추가 기능(코드플렉스)
Vs2017 및 Vs2019를 사용하는 가장 쉬운 방법을 보여드리겠습니다. Visual Studio에서 xsd를 열고 제안된 URL과 같이 샘플 xml 파일을 생성합니다.
- 아래와 같이 디자인 뷰에서 xsd를 열었으면 xml 스키마 탐색기를 클릭합니다.
"XML Schema Explorer"에서 아래로 스크롤하여 루트/데이터 노드를 찾습니다.루트/데이터 노드를 마우스 오른쪽 버튼으로 클릭하면 "샘플 XML 생성"이 표시됩니다.표시되지 않으면 데이터 요소 노드가 아니라 데이터 정의 노드에 있음을 의미합니다.
- 생성된 Xml을 클립보드에 복사
- 솔루션에 새 빈 클래스를 만들고 클래스 정의를 삭제합니다.네임스페이스만 남아 있어야 합니다.
- 마우스 포인터가 클래스 내부에 집중되어 있는 동안 EDIT-> Paste Special-> Paste Xml as Class를 선택합니다.
VS를 전혀 사용하지 않고 신속하고 게으른 솔루션을 사용하려면 다음 온라인 변환기를 사용해 보십시오.
- 여기서 xsd-to-xml-vlan
- xmltocsarp 변환기가 여기 있습니다.
XSD => XML => C# 클래스
예: XSD:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
XML로 변환합니다.
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Technologies Online Tools 1.0 (https://www.liquid-technologies.com) -->
<shiporder xsi:noNamespaceSchemaLocation="schema.xsd" orderid="string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<orderperson>string</orderperson>
<shipto>
<name>string</name>
<address>string</address>
<city>string</city>
<country>string</country>
</shipto>
<item>
<title>string</title>
<note>string</note>
<quantity>3229484693</quantity>
<price>-6894.465094196054907</price>
</item>
<item>
<title>string</title>
<note>string</note>
<quantity>2181272155</quantity>
<price>-2645.585094196054907</price>
</item>
<item>
<title>string</title>
<note>string</note>
<quantity>2485046602</quantity>
<price>4023.034905803945093</price>
</item>
<item>
<title>string</title>
<note>string</note>
<quantity>1342091380</quantity>
<price>-810.825094196054907</price>
</item>
</shiporder>
이 클래스 구조로 변환됩니다.
/*
Licensed under the Apache License, Version 2.0
http://www.apache.org/licenses/LICENSE-2.0
*/
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
[XmlRoot(ElementName="shipto")]
public class Shipto {
[XmlElement(ElementName="name")]
public string Name { get; set; }
[XmlElement(ElementName="address")]
public string Address { get; set; }
[XmlElement(ElementName="city")]
public string City { get; set; }
[XmlElement(ElementName="country")]
public string Country { get; set; }
}
[XmlRoot(ElementName="item")]
public class Item {
[XmlElement(ElementName="title")]
public string Title { get; set; }
[XmlElement(ElementName="note")]
public string Note { get; set; }
[XmlElement(ElementName="quantity")]
public string Quantity { get; set; }
[XmlElement(ElementName="price")]
public string Price { get; set; }
}
[XmlRoot(ElementName="shiporder")]
public class Shiporder {
[XmlElement(ElementName="orderperson")]
public string Orderperson { get; set; }
[XmlElement(ElementName="shipto")]
public Shipto Shipto { get; set; }
[XmlElement(ElementName="item")]
public List<Item> Item { get; set; }
[XmlAttribute(AttributeName="noNamespaceSchemaLocation", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
public string NoNamespaceSchemaLocation { get; set; }
[XmlAttribute(AttributeName="orderid")]
public string Orderid { get; set; }
[XmlAttribute(AttributeName="xsi", Namespace="http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
}
}
차려!이는 시작을 위한 것이므로 결과는 분명히 개선이 필요합니다!
순환 참조(즉, 유형이 직접 또는 간접적으로 자체 유형의 요소를 소유할 수 있음)가 있을 때는 xsd.exe가 제대로 작동하지 않습니다.
순환 참조가 있을 때는 Xsd2Code를 사용합니다.Xsd2Code는 순환 참조를 잘 처리하고 VSIDE 내에서 작동하므로 큰 이점이 있습니다.또한 직렬화/직렬화 코드 생성과 같은 많은 기능을 사용할 수 있습니다.GenerateX를 켜야 합니다.직렬화를 생성하는 경우에도 MLA 속성이 적용됩니다. 그렇지 않으면 모든 요소에 정의되지 않은 경우 주문에 대한 예외가 발생합니다.
두 가지 모두 선택 기능에서 잘 작동하지 않습니다.원하는 유형 대신 개체 목록/수집이 표시됩니다.xsd는 강력한 유형의 클래스로 직렬화/직렬화되지 않으므로 가능하면 선택을 피하는 것이 좋습니다.하지만 당신이 이것에 대해 신경쓰지 않는다면, 그것은 문제가 되지 않습니다.
xsd2 코드의 모든 기능은 시스템으로 역직렬화됩니다.Xml.XmlElement는 매우 편리하지만 강력한 유형의 객체를 원한다면 문제가 될 수 있습니다.사용자 지정 구성 데이터를 허용할 때 any를 사용하는 경우가 많기 때문에 XmlElement는 다른 곳에서 사용자 지정 정의된 다른 XML 역직렬화기로 전달하는 데 편리합니다.
사용합니다XSD생성할 배치 스크립트에서.xsd파일 및 클래스XML직접:
set XmlFilename=Your__Xml__Here
set WorkingFolder=Your__Xml__Path_Here
set XmlExtension=.xml
set XsdExtension=.xsd
set XSD="C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1\Tools\xsd.exe"
set XmlFilePath=%WorkingFolder%%XmlFilename%%XmlExtension%
set XsdFilePath=%WorkingFolder%%XmlFilename%%XsdExtension%
%XSD% %XmlFilePath% /out:%WorkingFolder%
%XSD% %XsdFilePath% /c /out:%WorkingFolder%
제 경우에 작동한 명령은 다음과 같습니다.
xsd /c your.xsd
자동 속성으로 클래스를 생성하려면 이를 사용하여 XSD를 XML로 변환한 다음 이를 사용하여 XML을 JSON으로 변환하고 결과를 클립보드에 복사합니다.그런 다음 VS에서 클래스가 생성될 파일 내에서 편집 > 붙여넣기 > JSON을 클래스로 붙여넣기로 이동합니다.
사용한xsd.exe윈도우즈 명령 프롬프트에 표시됩니다.
하지만, 내 xml이 여러 온라인 xml을 참조했기 때문에 (나의 경우)http://www.w3.org/1999/xlink.xsd어떤 것을 참조합니까?http://www.w3.org/2001/xml.xsd) 또한 이러한 스키마를 다운로드하여 xsd와 동일한 디렉토리에 저장한 다음 명령에 해당 파일을 나열해야 했습니다.
"C:\Program Files(x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\xsd.exe" /classes/language:CS your .xsd xlink.xsd xml.xsd
Marc Gravells의 답변은 저에게 맞았지만 제 xsd는 .xml 확장자였습니다.xsd 프로그램을 사용했을 때 다음과 같이 제공되었습니다.
- The table (Amt) cannot be the child table to itself in nested relations.
이 KB325695에 따라 확장명을 .xml에서 .xsd로 변경했고 잘 작동했습니다.
WSDL과 함께 xsd 파일도 가지고 있었습니다.위의 내용이 제 경우에는 작동하지 않아 오류가 오류가 발생했습니다.다음과 같이 작동했습니다.
wsdl /l:C# /out:D:\FileName.cs D:\NameApi\wsdl_1_1\RESAdapterService.wsdl
D:\CXTypes.xsd D:\CTypes.xsd
D:\Preferences.xsd
언급URL : https://stackoverflow.com/questions/5217665/how-to-generate-net-4-0-classes-from-xsd
'programing' 카테고리의 다른 글
| 제안/자동 완성을 통한 Excel 데이터 검증 (0) | 2023.05.22 |
|---|---|
| C# 어레이를 단일 값으로 채우는/인스턴스하는 방법은 무엇입니까? (0) | 2023.05.22 |
| 인증서 서명 요청을 얻는 방법 (0) | 2023.05.22 |
| 목록: 카운트 대 카운트() (0) | 2023.05.22 |
| URL 내부 어딘가에 있는 routerLink에 매개 변수를 전달하는 방법은 무엇입니까? (0) | 2023.05.22 |
