Two questions
-
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:
-
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? -
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:rootI 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 anXMLNamespaceManager
, add the default namespace as a namespace with a prefix, and then modify the XPath query to use the prefix, passing theXMLNamespaceManager
object to every call toselectNodes
orselectSingleNode
. Has anyone found a way to make the XPath query recognize the default namespace?
Thanks in advance. Richard
-
-
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:
-
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"&#x03A0;"
! Has anyone found a way to do this with the MSXML parser, or the .Net framework? -
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:rootI 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 anXMLNamespaceManager
, add the default namespace as a namespace with a prefix, and then modify the XPath query to use the prefix, passing theXMLNamespaceManager
object to every call toselectNodes
orselectSingleNode
. Has anyone found a way to make the XPath query recognize the default namespace?
Thanks in advance. Richard
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_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 WebThanks 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