0

感谢大家提出建议并在需要时提供帮助。

昨天我试图在 asp.net 4.0 中开发和 web 应用程序,我需要从 xml 解析数据并将其保存在数据库中。但在此之前,我还必须对其进行验证。

我尝试使用 .net 提供的工具 xsd.exe 生成架构文件,但我不知道它如何知道标记哪些节点或属性是强制性的?

就像在我的 xml 中一样,以下项目是强制性的

Root node <Market> 
<Login> and its sub element
<ProductType> and its <ProductTypeID/>
The attribute DML is mandatory but should have only 3 values NONE, PUT or MODIFY
<ProductType> may or may not have <ProductItem>
If <ProductItem> is present then it should have <ProductItemID>
<ProductItem> may or may not have <Brand>
If <Brand> is present then it should have <BrandID>

下面是我的xml

<?xml version="1.0" encoding="utf-8" ?>
<Market>
    <Login>
        <LoginId />
        <Password />        
    </Login>

    <ProductType DML="NONE">
        <ProductTypeID/>
        <Name/>
        <Detail/>       

        <ProductItem DML="PUT">
            <ProductItemID/>
            <Name/>
            <Detail/>

            <Brand DML="PUT">
                <BrandID/>
                <Name/>
                <Detail/>
            </Brand>

            <Brand DML="MODIFY">
                <BrandID/>
                <Name/>
                <Detail/>
            </Brand>
        </ProductItem>

        <ProductItem DML="MODIFY">
            <ProductItemID/>
            <Name/>
            <Detail/>
        </ProductItem>        
    </ProductType>
</Market>

我应该如何以及在哪里指定所有强制和可选参数,以便根据要求生成我的 xsd。

谢谢,

M。

4

2 回答 2

0

xsd.exe 只能尝试推断,您的 xml 中有哪些元素/属性,但它无法找出哪些信息是强制性的。但这是一个很好的起点。

使用图形 XML_Schema_Editor编辑生成的 xsd 以标记您的必填字段。这比学习 xsd 语言容易得多

于 2012-08-07T04:31:01.087 回答
0

我不认为 XSD 支持嵌套 XML。我会尝试将 XML 加载到一个XmlDocument并手动检查必填字段。

于 2012-08-07T09:07:28.313 回答