简体   繁体   English

使用xsd.exe创建具有包含架构的类对象

[英]creating class object with included schema using xsd.exe

I have received a XSD schema MainSchema.XSD and also Common.Xsd schema. 我已经收到一个XSD架构MainSchema.XSD以及Common.Xsd架构。

In MainSchema.xsd i have a following line: 在MainSchema.xsd中,我有以下一行:

<xs:include schemaLocation="Common.xsd"/>

And Common.Xsd hold a definition for various types of data like: Common.Xsd拥有各种数据的定义,例如:

<xs:simpleType name="SSN">
    <xs:annotation>
        <xs:documentation>Social security number is 10 digits</xs:documentation>
        <xs:appinfo>
            <altova:exampleValues>
                <altova:example value="5412983209"/>
                <altova:example value=""/>
            </altova:exampleValues>
        </xs:appinfo>
    </xs:annotation>
    <xs:restriction base="xs:string">
        <xs:whiteSpace value="collapse"/>
        <xs:pattern value="([0-9]{10})?"/>
    </xs:restriction>
</xs:simpleType>

and in MainSchema i have a property called SSNField of type SSN: 在MainSchema中,我有一个名为SSNField的SSN类型的属性:

<xs:attribute name="CompanySSN" type="SSN">
    <xs:annotation>
        <xs:documentation>SSN number of Company</xs:documentation>
    </xs:annotation>
</xs:attribute>

When i create ac# object class with this command: 当我使用此命令创建ac#对象类时:

xsd.exe -c -l:c# MainSchema.xsd Common.Xsd xsd.exe -c -l:c#MainSchema.xsd Common.Xsd

it then created a object called: 然后创建了一个名为:

MainSchema_Common.cs MainSchema_Common.cs

When i validate an object against this Schema it comes up with an Exception: 当我针对此架构验证对象时,它会抛出异常:

{"Type 'http://schemas.domain.com:SSN' is not declared, or is not a simple type."} {“类型'http://schemas.domain.com:SSN'未声明,或者不是简单类型。”}

Does anyone knows what i´m doing wrong ? 有人知道我在做什么错吗?

Bear in mynd that i received this XSD schemas from a outside source and i was told that there were no errors in this files. 请记住,我是从外部来源收到此XSD架构的,并且被告知该文件中没有错误。

Sincerly agh 真诚的啊

You need to explain how you are validating. 您需要说明如何进行验证。 I assume you are creating an instance of the class and then serializing to XML, and the xml is not validating? 我假设您正在创建该类的实例,然后序列化为XML,并且xml无法验证?

You need to be aware that just because your xml is the product of serializing a type derived using xsd.exe does not automatically mean the xml will be compliant to the schema. 您需要意识到,仅仅因为您的xml是序列化使用xsd.exe派生的类型的产品,并不意味着xml将自动符合该架构。

You may need to prime the XmlSerializer by injecting an override for the root namespace or indeed other nodes in the document. 您可能需要通过为根名称空间或文档中其他节点注入替代来准备XmlSerializer。

For example to inject a namespace at a certain node: 例如,要在某个节点上注入名称空间:

XmlAttributeOverrides overrides = new XmlAttributeOverrides();

var elementAttribute = new XmlElementAttribute(typeof(SSN))
{
    ElementName = "SSN", 
    Namespace = "http://schemas.domain.com:SSN"
};

var newAttribute = new XmlAttributes();
newAttribute.XmlElements.Add(elementAttribute);
overrides.Add(typeof(ParentNodeType), "SSN", newAttribute);

To call the serilaizer: 调用序列化器:

XmlSerializer serializer = new XmlSerializer(typeof(MyType), overrides); 

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM