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

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

?? ??? ??????

??????

???? ??? ??????? ????? ????? ????? ??? ????????? (DTD) ?? ???? "??????? ???????" ?????? XML Microsoft "(XDR) ?? ???? ??? ????? ???? XML (XSD) ??? ???????? ????? ???????? ???????. ???? ??? ??????? ????? ????? ??????? ??? XmlValidatingReader ?????? ?? ??? ????? XML ?? ????? ????? ?????? ???????? ??? ????? ??????? ??? XmlSchemaCollection ??? ?????? ????? ??????? ?????? ?? ??????? ?????? XML ?????? ?? ?????.

??????? XML ????? ??? ????? ?????. ???? ????? ???? ?????? ????? ?????? ???????? ??? ????????? ?????????. ?????? ??????? ?? ???? XML ???????? ????? ????? DTD ?? ???? XDR ???? XSD. ?????? XSD ??????? ??????? ?????? grammars XML ?? Microsoft .NET Framework ???? DTDs ???? ?????? 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. ????? ??????? DTD ??????? ??? ????? ???? ?????? ????? XML:
    <!ELEMENT Product (ProductName)>
    <!ATTLIST Product ProductID CDATA #REQUIRED>
    <!ELEMENT ProductName (#PCDATA)>
    					
  3. ?? ???? ????? ?? Product.dtd ?? C:\MyFolder ??????.
  4. ???? Product.xml ?? Visual Studio .NET.
  5. ????? ????? DOCTYPE ??????? ??? ???? Product.xml ???? ??? DTD ?? ????? XML:
    <?xml version="1.0"?>
    <!DOCTYPE Product SYSTEM "Product.dtd">
    					
  6. ??? ????? XML ??????? ProductWithDTD.xml.

?????? DTD ?????? ?? ??? ????? XML

  1. ?? Visual Studio .NET ? ????? ???? Visual C# "????" ?????? ValidateXml ??????? ???????.
  2. ????? ?????? ??????? ????? ??? ????? Class1.cs ??? ???:
    using System.Xml;        // for XmlTextReader and XmlValidatingReader
    using System.Xml.Schema; // for XmlSchemaCollection (which is used later)
    					
  3. ?? Class1.cs ? ????? ????? ?????? ??????? isValid ??? ????? ??????? ??????? ??? ???:
    private static bool isValid = true;      // If a validation error occurs,
                                             // set this flag to false in the
                                             // validation event handler. 
  4. ????? ???? XmlTextReader ?????? ????? XML ?? ??? ??? ?? ??????? ??????? ?? ?? ?????? XmlValidatingReader ?????? ?? ??? ?????? XML ??? ??? ???:
    XmlTextReader r = new XmlTextReader("C:\\MyFolder\\ProductWithDTD.xml");
    XmlValidatingReader v = new XmlValidatingReader(r);
    					
  5. ???? ??????? ValidationType ?????? XmlValidatingReader ??? ?????? ?? ????? ???????? (DTDXDR ?? ????????). ????? ??? ??????? ??? DTD ??? ???:
    v.ValidationType = ValidationType.DTD;
    					
  6. ?? ???? ???? ?? ????? ?????? ?? ????? ?????? ?????? ?????? ??? ?????? ?? ???. ?? ?????? ????????? ???????? ??????? ????? ????? ????? ?????? ?? ??? (???? ????? ????? MyValidationEventHandler ?? ?????? 8):
    v.ValidationEventHandler += 
       new ValidationEventHandler(MyValidationEventHandler);
    					
  7. ?? ?????? ????????? ???????? ??????? ????? ?? ?????? ?? ??? XML ???????. ?? ???? ???? ?? ????? ?????? ?? ????? ???? MyValidationEventHandler ??????? ?????. ??? ??????? ???? isValid ??? false (???? ?????? 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 ??? ??????? ??????? ??? ???:
    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. ????? ??????? ?????? XDR ??????? ??? ????? ???? ?????? ????? XML:
    <?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. ?? ???? ????? ?? Product.xdr ?? C:\MyFolder ??????.
  4. ???? ????? Product.xml ?????? ?? ??? XDR ?????? ??? ???:
    <?xml version="1.0"?>
    <Product ProductID="123" xmlns="x-schema:Product.xdr"> 
       <ProductName>Rugby jersey</ProductName>
    </Product>
    					
  5. ??? ????? XML ??????? ProductWithXDR.xml.

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

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

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

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

  1. ?? Visual Studio .NET ? ?? ?????? ??? ?? ????.
  2. ????? ??????? ???? XSD ??????? ??? ????? ???? ?????? ????? XML:
    <?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. ?? ???? ????? ?? Product.xsd ?? C:\MyFolder ??????.
  4. ???? Product.xml ?????? ?? ??? XSD ?????? ??? ???:
    <?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. ??? ????? XML ??????? ProductWithXSD.xml.

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

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

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

  1. ?? Visual Studio .NET ? ???? ProductWithXSD.xml.
  2. ???? ?????? ????? ??? ???????? urn: 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. ??? ????????? ??? ProductWithXSD.xml.
  4. ???? Product.xsd. ????? ????? ????? <xsd:schema> ??? ??? ???? ??? ????? ?????? ??? ????? ?????urn: MyNamespace:
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                targetNamespace="urn:MyNamespace"
                elementFormDefault="qualified">
    					
  5. ??? ????????? ??? Product.xsd.
  6. ?????? ??????? ??????? ???????? ???? XSD ?????? ?? ??? ????? XML.

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

  1. ?? Visual Studio .NET ? ???? Class1.cs. ????? ???? XmlSchemaCollection ?? ????? ??????? ??????? ??? ???:
    XmlSchemaCollection cache = new XmlSchemaCollection();
    					
  2. ???? ?? ?????? XmlSchemaCollection ???????? ????? ??????? ?????? ?? ??????? ?????? ??? ???? ????. ????? ?? ???? ????? ??? ??????. ?? ?????? ????????? ???????? ??????? ??? ????? ??????? ?????? Product.xsd:
    cache.Add("urn:MyNamespace", "C:\\MyFolder\\Product.xsd");
    					
  3. ????? ??????? ??????? ??? ???????? ???????? ?????? ???? XmlValidatingReader ?????? ????? ??????? ?????? ?????? XmlValidatingReader ???? ???? ?????? ??????? ???????? ?? ???????:
    v.Schemas.Add(cache);
    					

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

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

???????

???? ???????: 307379 - ????? ??? ??????: 03/?? ??????/1426 - ??????: 3.3
????? ???
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
????? ??????: 
kbmt kbhowtomaster KB307379 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????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