Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Has anyone browsed a folder for a schema to add to an XML document stored in DOM, then added that schema to the document and successfully validated the document with it, or included a URL for a default namespace/schema on a new document?

Neither samples listed here actually work.

C++
Get_Schema();
 tmpstr = DocNamespace;

 tmpstr +=" ";
 tmpstr += SchemaName;

att = doc->CreateAttribute("xsi:schemaLocation");
att->Value = tmpstr;
node->Attributes->Append(att);

void Get_Schema()
{
    OpenFileDialog^ openfiledialog = gcnew OpenFileDialog();
    openfiledialog->DefaultExt = L"xsd";


    if (openfiledialog->ShowDialog() ==  System::Windows::Forms::DialogResult::OK)
    {
        SchemaName = openfiledialog->FileName;
        SchemaBar->Text = "Schema: " + SchemaName;

        try
        {
          doc->Schemas->Add(DocNamespace,SchemaName);
        }
        catch (Exception ^e)
        {
          MessageString = "Error adding schema: " + e->Message;
          MessageBox::Show(MessageString);
        }
    }
}

And here's an effort to add a default schema to a new document:
C++
             if (ArgList->Last == nullptr)
             {
                 XmlNode ^tmp;
                 XmlAttribute ^att;
                 String ^tmpstr;
                 tmp = ArgDoc->CreateXmlDeclaration("1.0","UTF-8","");
                 ArgDoc->AppendChild(tmp);
                 tmp = ArgDoc->CreateComment("Test XML created by IE315_Editor");
                 ArgDoc->AppendChild(tmp);

                 att = ArgDoc->CreateAttribute("xmlns");
                 att->Value = "http://www.gph.ie/schemas/ics/IE315/v1";
                 ArgChild->Attributes->Append(att);

                 att = ArgDoc->CreateAttribute("xmlns:xsi");
                 att->Value =  "http://www.w3.org/2001/XMLSchema-instance";
                 ArgChild->Attributes->Append(att);

                 tmpstr = "http://www.gph.ie/schemas/ics/IE315/v1"; // the namespace
                 DocNamespace = tmpstr;
                 //V1.04 - Add the new namespace to the document
                 nsmgr = gcnew XmlNamespaceManager (ArgDoc->NameTable);
                 nsmgr->AddNamespace(L"xsi","http://www.w3.org/2001/XMLSchema-instance");

                 namespace_found = true;
                 // if there is no local schema specified, include a default.
                 if (SchemaName == "")
                 {
                     tmpstr +=  " http://gphost2:38711/xmlreferencesite/customs/ics/IE315-EntrySummaryDeclaration/v1/schema.xsd";
                     SchemaBar->Text = "Schema: http://gphost2:38711/xmlreferencesite/customs/ics/IE315-EntrySummaryDeclaration/v1/schema.xsd";
                     SchemaName = "http://gphost2:38711/xmlreferencesite/customs/ics/IE315-EntrySummaryDeclaration/v1/schema.xsd";
                     // V1.04 Addition of DEBUG_SCHEMA
#ifdef DEBUG_SCHEMA
                     MessageString = "NOTE: Using DEBUG Schema!!!";
                        MessageBox::Show(MessageString);
                     //C:/GPH/Schemas/IE315/v1/schema.xsd
                     tmpstr +=  " C:/GPH/Schemas/IE315/v1/schema.xsd";
                     SchemaBar->Text = "Schema: C:/GPH/Schemas/IE315/v1/schema.xsd";
                     SchemaName = "C:/GPH/Schemas/IE315/v1/schema.xsd";
#endif

                     // V1.04
                     //XmlSchemaCollection^ xsc = gcnew XmlSchemaCollection;
                     //xsc->Add(DocNamespace, SchemaName);
                     ArgDoc->Schemas->Add(DocNamespace,SchemaName);
                     ArgDoc->Schemas->ValidationEventHandler+=eventHandler;
                     nsmgr->AddNamespace(L"ie", DocNamespace);


                     schema_found = true;
                 }
Posted
Updated 18-Mar-11 13:04pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900