XPathNavigator not working when schema used

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Obe

    XPathNavigator not working when schema used

    PLEASE HELP!

    I'm writing a function to populate DropDownLists from and xml file. I
    am having a problem getting my XPathNavigator to select any nodes when
    I attach a schema to my xml document.

    Xml Doc: Dropdown.xml
    XML Schema: Dropdown.xsd

    Here is my XML Document...

    <?xml version="1.0" encoding="utf-8" ?>
    <DropDownList s xmlns="http://tempuri.org/DropDown.xsd">
    <DropDownList name="DataSourc e">
    <Option name="SqlServer " value="SqlServe r" />
    <Option name="Oracle" value="Oracle" />
    <Option name="DB2" value="DB2" />
    </DropDownList>
    <DropDownList name="Language" >
    <Option name="ASP" value="ASP" />
    <Option name="ASP.Net" value="ASP.Net" />
    </DropDownList>
    </DropDownLists>

    ....and here is my code...

    private void LoadDropdownMen u(string DropDownName)
    {
    DropDownList GenericDropDown =
    (DropDownList)F indControl(Drop DownName);

    XPathDocument xpDoc = new XPathDocument(S erver.MapPath(" .") +
    "\\Dropdown.xml ", XmlSpace.Preser ve);
    XPathNavigator xNav = xpDoc.CreateNav igator();

    XPathNodeIterat or xNode =
    xNav.Select("/DropDownLists/DropDownList[@name='" + DropDownName +
    "']/Option");

    while(xNode.Mov eNext())
    {
    GenericDropDown .Items.Add(new
    ListItem(xNode. Current.GetAttr ibute("name","" ),xNode.Current .GetAttribute(" value","http://tempuri.org/DropDown.xsd")) );
    }
    }

    I don't get any nodes returned in my XPathNodeIterat or using the above
    xml file and code. If I remove xmlns="http://tempuri.org/DropDown.xsd"
    from my xml file, everything works perfectly.

    I want to be able to use a schema with my xml file. Can someone please
    explain what I am doing wrong?

    Thanks in advance.
  • Bjoern Hoehrmann

    #2
    Re: XPathNavigator not working when schema used

    * Obe wrote in microsoft.publi c.dotnet.xml:[color=blue]
    >I'm writing a function to populate DropDownLists from and xml file. I
    >am having a problem getting my XPathNavigator to select any nodes when
    >I attach a schema to my xml document.[/color]

    You are not attaching a schema to the document, you are declaring the
    namespace of the elements in your document. An XPath expression ala
    //name refers to all "name" elements in no namespace, this fails if
    the element is in a namespace. You need to declare a prefix for the
    namespace and use that prefix in your XPath expressions. You can do
    that using XmlNamespaceMan ager, see the Remarks section in e.g.

      http://msdn.microsoft.com/library/en...lecttopic2.asp

    for an example and further details. The XML Namespaces FAQ might also
    be helpful to resolve further issues involving namespaces,


    Comment

    Working...