????? C# .NET ??? DTD, XDR ?? XSD ?? ????? ?? ???? XML ???????? ????? ???? ???? ????

???? ?????? ???? ??????
???? ID: 307379 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

????????? ???? ?? ?? ???? ?? ???? ?? ???? ???? ?? ??? ??? ???????? ?????? ???????? (DTD), ???? Microsoft XML ???? ????? (XDR) ??????, ?? ???? XML ?????? ??????? ???? (XSD) ?????? ?? ??? ??? ??????????? ??????? ???? (XML) ????????? ?? ???? ????? ?? ????? ???? ?? ??? ???? ?? ???? ??XmlValidatingReader????????? ???????, ??? ?? ?? ????? ???? ???? ?? ??????? ??? XML ???????? ?? ????? ???? ?? ??? ????XmlSchemaCollection???? ?? XML ????????? ???? ?? ??? ?????? ??? ??? ?????? ????????

XML ???????? ??? ???? ?? ????????? ???? ?? ??????????? ?? ??????? ?? ??? ???? ?????? ???? ?? ??? ?? ????? ?? ????? ????? ?????? ????? ???? ????? ?????? ?? ???? XML ?? ??????? ?? ????????? ???? ?? ??? ???????? ?? ???, ?? ??? ???? ??? DTD, ?? XDR ??????, ?? ??? XSD ??????? XSD ?????? ??? Microsoft .NET Framework ????? DTDs ??? XML grammars ?? ????????? ???? ?? ??? ?????? ??????? ???? ??, ?? XDR ?????? ?? ??????? ????

??????????

?? ???? ????? ?? ?? ????? ???? ?? ??????:
  • Microsoft Visual C# .NET ?? Microsoft Visual Basic .NET ????????
  • XML ?????????, ??????? ?? ???????? ?? ????? ????

??? XML ???????? ?????

  1. Visual Studio .NET ?? ??????? ?????
  2. ???? ??????? ???????? ?? ??? ??? XML ???? ??????
  3. ????? ???? ??? ??? ?????? ??? ??? ?????? ?? ???????????? ???? ?? ??? XML ???????? ??? ??????:
    <Product ProductID="123">
       <ProductName>Rugby jersey</ProductName>
    </Product>
    					
  4. ???? ?? ???? ?? ??????? ??? Product.xml ?? ??? ??? ?????? C:\MyFolder ????

DTD ?? ????? ????

DTD ?????, ?? XML ???????? ??? ???? ????

  1. Visual Studio .NET, ???? ???? ??? ???? ??????
  2. XML ???????? ?? ??????? ?? ????? ???? ?? ??? ????? ?? ??? ????? DTD declarations ??????:
    <!ELEMENT Product (ProductName)>
    <!ATTLIST Product ProductID CDATA #REQUIRED>
    <!ELEMENT ProductName (#PCDATA)>
    					
  3. ????? ?? C:\MyFolder ??? Product.dtd ?? ??? ??? ?????? ???????????
  4. Visual Studio .NET ??? Product.xml ?? ??????
  5. ????? ??????DOCTYPEstatement to the top of Product.xml to link the XML document to the DTD file:
    <?xml version="1.0"?>
    <!DOCTYPE Product SYSTEM "Product.dtd">
    					
  6. Save the modified XML document as ProductWithDTD.xml.

Use the DTD to Validate the XML Document

  1. In Visual Studio .NET, create a new Visual C# Console Application project named ValidateXml.
  2. ?? ???????? ????? ????statements to the beginning of Class1.cs as follows:
    using System.Xml;        // for XmlTextReader and XmlValidatingReader
    using System.Xml.Schema; // for XmlSchemaCollection (which is used later)
    					
  3. In Class1.cs, declare a boolean variable namedisValidbefore the start of the?????follows ?? ??? ??? ????:
    private static bool isValid = true;      // If a validation error occurs,
                                             // set this flag to false in the
                                             // validation event handler. 
  4. Create anXmlTextReaderobject to read an XML document from a text file in the?????method, and then create anXmlValidatingReaderto validate this XML data as follows:
    XmlTextReader r = new XmlTextReader("C:\\MyFolder\\ProductWithDTD.xml");
    XmlValidatingReader v = new XmlValidatingReader(r);
    					
  5. TheValidationType?? ???XmlValidatingReader???????? ?????? ?? ?????????? ?????? (?? ?? ????? ???? ??DTD,XDR??,??????). ?? ??? ??? ????DTD???????????:
    v.ValidationType = ValidationType.DTD;
    					
  6. ??? ??? ?? ??????? ????????? ???????, validating ???? ???? ??????? ????? ?? ????? ??? ??? ?????????? ????? ?????? ??????? ???? ?? ??? ????? ??? ?????? (??????????? ?????MyValidationEventHandler???? ??? 8 ???):
    v.ValidationEventHandler += 
       new ValidationEventHandler(MyValidationEventHandler);
    					
  7. ????? ?? XML ????? ???? ?? ??? ????? ??? ?????? ????????? ??? ??? ?? ??????? ????????? ??????? ????MyValidationEventHandler?????? ?? ??? ???? ?? ??? ??? ??? ?? ???? ?? ??? ???? ??isValid?? ??? ??? (??? 8 ?????)? ?????? ?? ???? ?? ???? ???isValid??? ?? ????? ?? ??? ??????? ?? ??? ???????? ????? ?? ?????? ???
    while (v.Read())
    {
       // Can add code here to process the content.
    }
    v.Close();
    
    // Check whether the document is valid or invalid.
    if (isValid)
       Console.WriteLine("Document is valid");
    else
       Console.WriteLine("Document is invalid");
    					
  8. ?????MyValidationEventHandler???? ??? ?????????follows ?? ??? ??? ????:
    public static void MyValidationEventHandler(object sender, 
                                                ValidationEventArgs args) 
    {
       isValid = false;
       Console.WriteLine("Validation event\n" + args.Message);
    }
    					
  9. ?????, ?? ????????? ?????? ????????? ??????? ???? ????? ?? XML ???????? ????? ???
  10. Visual Studio .NET, ??? ?? ?????? ???? ?? ??? ProductWithDTD.xml ??????? (?????? ?? ???, ????? "<productname>Rugby jersey</productname>" ????)?
  11. ????????? ?? ???: ?????? ???? ????? ?????? ????? ??????? ???? ?????:
    ??????? ?????
    '??????' ???? ??? ?????? ??????? ??? 'ProductName' ?????????
    File:///C:/MyFolder/ProductWithDTD.xml(4, 5) ?? ??? ?????? ??????? ????
    ????????? ?????? ??

XDR ?????? ?? ????? ????

XDR ?????? ?????, ?? XML ???????? ??? ???? ????

  1. Visual Studio .NET, ???? ???? ??? ???? ??????
  2. XML ???????? ?? ??????? ?? ????? ???? ?? ??? ????? ?? ??? ????? XDR ?????? ????????? ??????:
    <?xml version="1.0"?>
    <Schema name="ProductSchema" 
            xmlns="urn:schemas-microsoft-com:xml-data"
            xmlns:dt="urn:schemas-microsoft-com:datatypes">
    
       <ElementType name="Product" content="eltOnly">
          <attribute type="ProductID" required="yes"/>
          <element type="ProductName"/>
       </ElementType>
    
       <AttributeType name="ProductID" dt:type="int"/>
       <ElementType name="ProductName" dt:type="string"/>
    </Schema>
    					
  3. ????? ?? C:\MyFolder ??? Product.xdr ?? ??? ??? ?????? ???????????
  4. ??? Product.xml ???? ?????, ?? XDR ???? ?? ??? ?? ???? follows ?? ??? ??? ??????:
    <?xml version="1.0"?>
    <Product ProductID="123" xmlns="x-schema:Product.xdr"> 
       <ProductName>Rugby jersey</ProductName>
    </Product>
    					
  5. ProductWithXDR.xml ?? ??? ??? ??????? XML ???????? ?? ???????

XML ???????? ?? ????? ???? ?? ??? XDR ?????? ?? ????? ????

  1. ???? ????????? ?? ??????? ???? ????XmlTextReader?? ??? ??? ProductWithXDR.xml ??? ????:
    XmlTextReader r = new XmlTextReader("C:\\MyFolder\\ProductWithXDR.xml");
    					
  2. ??? ????ValidationType???? ?? ???XDR????? ?? validating ???? XDR ??????? ?? ????????? ???? ??:
    v.ValidationType = ValidationType.XDR;
    					
  3. ?????, ?? ????????? ?????? ????????? ??????? ???? ????? ?? XML ???????? ????? ???
  4. ?????? ??, ?? ???? ?? ??? ProductWithXDR.xml ??????? ?????
  5. ?????, ?? ????????? ?? ???: ?????? ?? ???? ??????? ?????? ??????? ???? ??????

XSD ?????? ?? ????? ????

XSD ?????? ?????, ?? XML ???????? ??? ???? ????

  1. Visual Studio .NET, ???? ???? ??? ???? ??????
  2. XML ???????? ?? ??????? ?? ????? ???? ?? ??? ????? ?? ??? ????? XSD ?????? ????????? ??????:
    <?xml version="1.0"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
       <xsd:element name="Product">
          <xsd:complexType>
             <xsd:sequence>
                <xsd:element name="ProductName" type="xsd:string"/>
             </xsd:sequence>
             <xsd:attribute name="ProductID" use="required" type="xsd:int"/>
          </xsd:complexType>
       </xsd:element>
    </xsd:schema>
    					
  3. ????? ?? C:\MyFolder ??? Product.xsd ?? ??? ??? ?????? ???????????
  4. ??? Product.xml ?????, ?? ??? XSD ???? ?? ??? ???? follows ?? ??? ??? ??????:
    <?xml version="1.0"?>
    <Product ProductID="123" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:noNamespaceSchemaLocation="Product.xsd">
       <ProductName>Rugby jersey</ProductName>
    </Product>
    					
  5. ProductWithXSD.xml ?? ??? ??? ??????? XML ???????? ?? ???????

XML ???????? ?? ????? ???? ?? ??? XSD ?????? ?? ????? ????

  1. ???? ????????? ?? ??????? ???? ????XmlTextReader?? ??? ??? ProductWithXSD.xml ??? ????:
    XmlTextReader r = new XmlTextReader("C:\\MyFolder\\ProductWithXSD.xml");
    					
  2. ??? ????ValidationType???? ?? ?????????????? ?? validating ???? XSD ?????? ??????? ?? ????????? ???? ??:
    v.ValidationType = ValidationType.Schema;
    					
  3. ?????, ?? XML ???????? ?? ????? ???? ?? ??? XSD ?????? ?? ????? ???? ?? ??? ????????? ??????

???????? ??? XSD ?????? ?? ????? ????

  1. Visual Studio .NET, ProductWithXSD.xml ??????
  2. ??? ??????? ??? ????? ?? ??????: MyNamespace??? ???????? ??? ???? ?????, ??????? XSD ???? ?? ?? ?? ??? ????? ??? ??????? ?? ????? ???? ?? ??? XSD ?????? ????????? ???? ??:
    <?xml version="1.0"?>
    <Product ProductID="123" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="urn:MyNamespace"
             xsi:schemaLocation="urn:MyNamespace Product.xsd">
       <ProductName>Rugby jersey</ProductName>
    </Product>
    					
  3. Save your changes to ProductWithXSD.xml.
  4. Open Product.xsd. Modify the <xsd:schema> start tag as follows so that the schema applies to the namespace </xsd:schema>urn:MyNamespace:
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                targetNamespace="urn:MyNamespace"
                elementFormDefault="qualified">
    					
  5. Save your changes to Product.xsd.
  6. Build and run the application to use the XSD schema to validate the XML document.

Cache Namespaces

  1. In Visual Studio .NET, open Class1.cs. Create anXmlSchemaCollectionobject at the beginning of the?????follows ?? ??? ??? ????:
    XmlSchemaCollection cache = new XmlSchemaCollection();
    					
  2. TheXmlSchemaCollectionobject allows you to cache schemas in memory for improved performance. Each schema is associated with a different namespace. Add the following code to cache Product.xsd:
    cache.Add("urn:MyNamespace", "C:\\MyFolder\\Product.xsd");
    					
  3. Add the following statement after the code that creates theXmlValidatingReader???????? ?? ??? ?????? ??? ?? ?????? ?? ???XmlValidatingReader????? ?? ???? ???-?????? ?????? ?? ????? ?? ???? ???:
    v.Schemas.Add(cache);
    					

???? ???? ?? ??? ???

  1. ?????, ?? ??? ????????? ??????
  2. ???????? ???? ?? XML ???????? ?? ??? ?? ????? XSD ?????? ?? ??????? ???
  3. ?????? ??, ?? ???? ?? ??? ProductWithXSD.xml ??????? ?????
  4. ???????? ???? ?? ????????? ?? ??????? ????????? ?? ??? ????? ???

???

???? ID: 307379 - ????? ???????: 04 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
??????: 
kbhowtomaster kbmt KB307379 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:307379

??????????? ???

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com