Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. Two questions

Two questions

Scheduled Pinned Locked Moved XML / XSL
xmlcsharphtmldatabasedotnet
3 Posts 2 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Richard DeemingR Offline
    Richard DeemingR Offline
    Richard Deeming
    wrote on last edited by
    #1

    OK, this is probably me being stupid, but there are two things I want to do, and I can't seem to get them working:

    1. Insert special characters with the DOM Some Unicode characters don't get encoded properly by the DOM. I've set the encoding to UTF-8, but when I set the Text property of a node to a string containing Unicode characters, they get replaced with "?". I've tried replacing them with the relevant "Π", but that gets encoded as "Π"! Has anyone found a way to do this with the MSXML parser, or the .Net framework?

    2. Query the default namespace with XPath If I have an XML document with no namespace:

      <?xml version='1.0'?>
      <root>
      <someNode>Item 1</someNode>
      <someNode>Item 2</someNode>
      <someNode>Etc...</someNode>
      <root>

      I can use oNode.selectNodes("//someNode") to select the "someNode" nodes. If I have a namespace with a prefix:

      <?xml version='1.0'?>
      <trinet:root xmlns:trinet='http://www.trinet.co.uk/xml/Test.xsd'>
      trinet:someNodeItem 1</trinet:someNode>
      trinet:someNodeItem 2</trinet:someNode>
      trinet:someNodeEtc...</trinet:someNode>
      trinet:root

      I can use oNode.selectNodes("//trinet:someNode"). But, if I have a default namespace:

      <?xml version='1.0'?>
      <root xmlns='http://www.trinet.co.uk/xml/Test.xsd'>
      <someNode>Item 1</someNode>
      <someNode>Item 2</someNode>
      <someNode>Etc...</someNode>
      <root>

      oNode.selectNodes(""//someNode") returns nothing! So far, the only solution I have found (in .Net) is to create an XMLNamespaceManager, add the default namespace as a namespace with a prefix, and then modify the XPath query to use the prefix, passing the XMLNamespaceManager object to every call to selectNodes or selectSingleNode. Has anyone found a way to make the XPath query recognize the default namespace?

    Thanks in advance. Richard

    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

    E 1 Reply Last reply
    0
    • Richard DeemingR Richard Deeming

      OK, this is probably me being stupid, but there are two things I want to do, and I can't seem to get them working:

      1. Insert special characters with the DOM Some Unicode characters don't get encoded properly by the DOM. I've set the encoding to UTF-8, but when I set the Text property of a node to a string containing Unicode characters, they get replaced with "?". I've tried replacing them with the relevant "&#x03A0;", but that gets encoded as "&amp;#x03A0;"! Has anyone found a way to do this with the MSXML parser, or the .Net framework?

      2. Query the default namespace with XPath If I have an XML document with no namespace:

        <?xml version='1.0'?>
        <root>
        <someNode>Item 1</someNode>
        <someNode>Item 2</someNode>
        <someNode>Etc...</someNode>
        <root>

        I can use oNode.selectNodes("//someNode") to select the "someNode" nodes. If I have a namespace with a prefix:

        <?xml version='1.0'?>
        <trinet:root xmlns:trinet='http://www.trinet.co.uk/xml/Test.xsd'>
        trinet:someNodeItem 1</trinet:someNode>
        trinet:someNodeItem 2</trinet:someNode>
        trinet:someNodeEtc...</trinet:someNode>
        trinet:root

        I can use oNode.selectNodes("//trinet:someNode"). But, if I have a default namespace:

        <?xml version='1.0'?>
        <root xmlns='http://www.trinet.co.uk/xml/Test.xsd'>
        <someNode>Item 1</someNode>
        <someNode>Item 2</someNode>
        <someNode>Etc...</someNode>
        <root>

        oNode.selectNodes(""//someNode") returns nothing! So far, the only solution I have found (in .Net) is to create an XMLNamespaceManager, add the default namespace as a namespace with a prefix, and then modify the XPath query to use the prefix, passing the XMLNamespaceManager object to every call to selectNodes or selectSingleNode. Has anyone found a way to make the XPath query recognize the default namespace?

      Thanks in advance. Richard

      E Offline
      E Offline
      Erik Westermann
      wrote on last edited by
      #2

      Richard_D wrote: Insert special characters with the DOM There's something wrong on your system since XML supports Unicode characters. Make sure that your system supports the unicode characters you're inserting into the document. Assuming that you read the document into an editor to see the '?' marks in place of your unicode characters, make sure that the editor you use supports unicode files - Notepad on Windows 9x does not support Unicode whereas Notepad on Windows 2000 and XP supports it. If you're using 9x, open the file using something like XML Spy, which supports Unicode directly. Richard_D wrote: Query the default namespace with XPath You've run into a very common problem with XPath 1.0. The specification is very confusing with regards to how vendors should handle the default namespace; as a result, vendors implement it differenly. The next incarnation of XPath is epxected address the problem. In the mean time, your best option is to use a prefix as you did in your message's second listing. I suggest that you try to stick to that method since Microsoft could change their mind and cause your app to break when the next incarnation of XPath becomes a standard. Using namespace prefixes takes advantage of a standard, whereas the method you use (using the XMLNamespaceManager) is specific to Microsoft's XML parser and may not work in the future based on the direction of the wind and the state of evolving standards. Essam - Author, JScript .NET Programming
      ...and a bunch of articles around the Web

      Richard DeemingR 1 Reply Last reply
      0
      • E Erik Westermann

        Richard_D wrote: Insert special characters with the DOM There's something wrong on your system since XML supports Unicode characters. Make sure that your system supports the unicode characters you're inserting into the document. Assuming that you read the document into an editor to see the '?' marks in place of your unicode characters, make sure that the editor you use supports unicode files - Notepad on Windows 9x does not support Unicode whereas Notepad on Windows 2000 and XP supports it. If you're using 9x, open the file using something like XML Spy, which supports Unicode directly. Richard_D wrote: Query the default namespace with XPath You've run into a very common problem with XPath 1.0. The specification is very confusing with regards to how vendors should handle the default namespace; as a result, vendors implement it differenly. The next incarnation of XPath is epxected address the problem. In the mean time, your best option is to use a prefix as you did in your message's second listing. I suggest that you try to stick to that method since Microsoft could change their mind and cause your app to break when the next incarnation of XPath becomes a standard. Using namespace prefixes takes advantage of a standard, whereas the method you use (using the XMLNamespaceManager) is specific to Microsoft's XML parser and may not work in the future based on the direction of the wind and the state of evolving standards. Essam - Author, JScript .NET Programming
        ...and a bunch of articles around the Web

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        Thanks for replying. I think I've solved the first problem - I needed to set the encoding to "ISO-8859-1" instead of "UTF-8". Now the only problem is to get the XMLHTTP object to recognize the correct code-page for non-XML documents. As for XPath, I kind of suspected that this was the case. The other solution I found, which will only work for some XML documents, is to use the DataSet object to load the XML. Hopefully, this will continue to be supported as .Net evolves, regardless of the standards implemented. Cheers, Richard

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups