CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2002

    "element is not declared" when xsd on an xml file

    Can anyone tell me why when I atempt to validate my below xml file against the xsd file, I get the below error message?
    Many thanks.

    The 'ItemList' element is not declared. An error occurred at file:///g:/temp/WDWEBSVC.XML, (4, 2).: Message:{0}


    <?xml version="1.0" encoding="us-ascii" standalone="yes"?>
    <?xml-stylesheet version='1.0'?>
    <!--this file contains wdog xml data for all items-->
    <ItemList xmlns:il="http://tempuri.org/WDWEBSVC.xsd">
    <strItemName>Ping Nifty Tools</strItemName>
    <strItemName>Html download Nifty Tools</strItemName>
    <strItemType>HTML Download</strItemType>
    <strItemStatus>HTML Download</strItemStatus>
    <strItemName>Screen Saver</strItemName>
    <strItemType>NT Screen Saver</strItemType>
    <strItemStatus>NT Screen Saver</strItemStatus>


    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema id="ItemList" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    <xs:element name="ItemList" msdata:IsDataSet="true">
    <xs:choice maxOccurs="unbounded">
    <xs:element name="Item">
    <xs:element name="strItemName" type="xs:string" minOccurs="0" />
    <xs:element name="strItemAddress" type="xs:string" minOccurs="0" />
    <xs:element name="strItemType" type="xs:string" minOccurs="0" />
    <xs:element name="strItemStatus" type="xs:string" minOccurs="0" />


    MyXmlTextReader = new XmlTextReader ( strCheckFile );

    MyXmlValidatingReader = new XmlValidatingReader ( MyXmlTextReader );

    try {
    MyXmlValidatingReader.Schemas.Add ( strCheckUrn, strCheckSchema );

    MyXmlValidatingReader.ValidationType = ValidationType.Schema;
    MyXmlValidatingReader.ValidationEventHandler += new ValidationEventHandler ( ValidationCallback );

    try {
    // read whole file and check it

    m_bXmlFileIsValid = true;

    while ( MyXmlValidatingReader.Read() )
    if ( m_bXmlFileIsValid == false ) break; // abort if error

    return ( m_bXmlFileIsValid ); // set in ValidationCallback()
    catch ( Exception e )
    if ( MyXmlValidatingReader != null ) MyXmlValidatingReader.Close();

    Debug.WriteLine ( "Validate error: " + e.ToString() );

    m_bXmlFileIsValid = false;

    return ( m_bXmlFileIsValid );

  2. #2
    Join Date
    Oct 2004

    Re: "element is not declared" when xsd on an xml file

    Name space problem.
    Your Itemlist element is not in the http://tempuri.org/WDWEBSVC.xsd namespace.

    Easiest way to make it validate is add il: at the beginning of these elements in your xml file.
    <il:ItemList xmlns:il="http://tempuri.org/WDWEBSVC.xsd">

    not sure if that's exactly what you want.

  3. #3
    Join Date
    Jul 2002

    Re: "element is not declared" when xsd on an xml file

    Thank you.
    I added the il: and /il:

    and it now validates BUT i now have a new problem.
    Now my XPATH query fails with 0 items found.

    XmlDocument MyXmlDocument = new XmlDocument ();
    MyXmlDocument.Load ( strItemInfoXmlPathFileName );

    XPathNavigator MyXPathNagivator =
    MyXmlDocument.CreateNavigator ();

    MyXPathNagivator.MoveToRoot ();

    // select all items

    XPathNodeIterator MyXPathNodeIterator =
    MyXPathNagivator.Select ( "/ItemList/Item" );
    Debug.WriteLine ( "got " + MyXPathNodeIterator.Count +
    " items" );

    - returns 0 items when it should return 3 items ! help! See the xml data file in the previous post

  4. #4
    Join Date
    Oct 2004

    Re: "element is not declared" when xsd on an xml file

    I think what you want is to have all your elements inside a single namespace, correct? If not, please clarify.
    Requires 3 changes from your original.

    1. add elementFormDefault attribute to your schema.
    <xs:schema id="ItemList" xmlns:xs="http://www.w3.org/2001/XMLSchema"

    2. Change your xml to:
    <ItemList xmlns="http://tempuri.org/WDWEBSVC.xsd">
    Removed the :il after xmlns; this makes all elements within this namespace.

    3. Change code to take into account the namespace.
    Unfortunately, I don't know how to do this using the System.Xml.Xpath namespace. Is there any reason you're not using the simpler System.Xml classes such XMLNode and XMLDocument?

    If you were, you could find the nodes by adding a xmlNamespaceManager:
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(MyXmlDocument.NameTable);

    Change xpath when you search, prefix elements with the namespace tag, and add the namespace manager.
    MyXmlDocument.Select ( "/il:ItemList/il:Item" , nsmgr);

    that's the proper way. The easy improper way is to change everything to the default namespace, but then you lose data integrity.
    Last edited by jkmyoung; March 3rd, 2006 at 08:40 AM.

  5. #5
    Join Date
    Jul 2002

    cant get C# constructs right to read in an XML file via serialization

    this is my xml file i want to read in using .net deserialization
    I cna not figure out how to create a C# class or structure that when deserialized, will read this xml file.

    1. <?xml version="1.0" encoding="us-ascii" standalone="yes"?>
    2. <?xml-stylesheet version='1.0'?>
    3. <il:ItemList xmlns:il="http://tempuri.org/WDWEBSVC.xsd">
    4. <Item>
    5. <strItemName>Ping Nifty Tools</strItemName>
    6. <strItemAddress>www.niftytools.com</strItemAddress>
    7. </Item>
    8. <Item>
    9. <strItemName>Html download Nifty Tools</strItemName>
    10. <strItemAddress>http://www.niftytools.com</strItemAddress>
    11. </Item>
    12. <Item>
    13. <strItemName>Screen Saver</strItemName>
    14. <strItemAddress>http://infodeli.3com.com/index.htm</strItemAddress>
    15. </Item>
    16. </il:ItemList>

    I tried the belowand many other attempts but cant get it right.

    public struct structDataForItem {
    public string strItemName;
    public string strItemAddress;

    public class ItemList
    public structDataForItem [] Item;

    public ItemList ()
    Item = new structDataForItem [ 5 ]; <= i know 5 is not correct but an AreaList wont serialize to xml.

    generates the below:

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <ItemList xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    3. <Item>
    4. <structDataForItem>
    5. <strItemName>name0</strItemName>
    6. <strItemAddress>address0</strItemAddress>
    7. </structDataForItem>
    8. <structDataForItem>
    9. <strItemName>name1</strItemName>
    10. <strItemAddress>address1</strItemAddress>
    11. </structDataForItem>
    12. <structDataForItem />
    13. <structDataForItem />
    14. <structDataForItem />
    15. </Item>
    16. </ItemList>

    I DONT WANT the <<structDataForItem> to ever appear in the code. Any ideas in what C# classes/structs would work?

  6. #6
    Join Date
    Oct 2011
    Mesa, AZ

    Re: "element is not declared" when xsd on an xml file

    Use the xsd tool to generate an xsd of your xml file. This used to come with VS, but you should be able to download it here http://www.haiders.net/post/Download-xsdexe.aspx

    eg. xsd sample.xml 'This will generate samaple.xsd

    Now use the same tool to generate the Classes from sample.xsd

    eg xsd sample.xsd /c /l:cs 'The will generate the c# classes that serialize/de-serialize your xml file.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.