XML issue

  • Thread starter Thread starter C# newbie
  • Start date Start date
C

C# newbie

Hi Group,

I have a problem with XML! I have an xml file which starts like this:

<Plaftorm PlatformName="empty" PlatformID="1" StyleUsage="URL"
xmlns="http://www.domain.com" PatformProgrammer="mynamehere"
DateTime="09/12/2003 6:00:54 PM">
.......
......
</Plaform>

When I changed it to something like below, the XPath search works fine.

<Plaftorm >
.......
.....
</Plaform>

Any idea?

Thx
 
Hard to say, because:

- You've given us XML that isn't well-formed (although it looks like a
typo, you've done it twice).
- You haven't said what the problem is. Everybody has problems with XML.
However, the solution depends on exactly which particular problem you're
having ;)
 
Mickey,

It's not my xml. I've been told to do some process on it and when
I removed some part of header it worked just fine!
Again The problem is that when the XML starts with <Platfom> my XPath
function works good but in its native form no!
It ignores some of my queries!

Let me know if you need more info.

Thanks in advance
 
When you change from

<Plaftorm xmlns="http://www.domain.com"

to

<Platform ...

you change the default namespace. An XPath expression has to match the
namespace as well as the element name, so in the first example XPath like
select="/Platform" won't work because it does not specify the namespace that
the Platform element is in.
 
Just agreeing with the namespace issue.

I had this problem when first using XML and referred to this document:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexxml/html/xml05202002.asp

Taking your example, making one spelling correction for the closing root element, adding the necessary additions to support the namespace, and an extra element to test it I get this:

System.Xml.XmlDocument xd = new XmlDocument();

xd.InnerXml = "<Plaftorm PlatformName=\"empty\" PlatformID=\"1\" StyleUsage=\"URL\" xmlns:xslt=\"http://www.domain.com\" PatformProgrammer=\"mynamehere\" DateTime=\"09/12/2003 6:00:54 PM\"><xslt:a>datafora</xslt:a></Plaftorm>";
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xd.NameTable);
nsMgr.AddNamespace("xslt","http://www.domain.com");
XmlNodeList nodes = xd.SelectNodes("//xslt:a", nsMgr);
Console.WriteLine(nodes.Count);

Another option would be to remove the namespace from the xml completely:

xd.InnerXml = "<Plaftorm PlatformName=\"empty\" PlatformID=\"1\" StyleUsage=\"URL\" PatformProgrammer=\"mynamehere\" DateTime=\"09/12/2003 6:00:54 PM\"><a>datafora</a></Plaftorm>";
XmlNodeList nodes = xd.SelectNodes("//a");
Console.WriteLine(nodes.Count);

hope this helps.
 
Ash,

Thanks for your detailed answer really thanks. However, i have a
question about "a" ! what does "a" do in "//xslt:a"?

nsMgr.AddNamespace("xslt","http://www.domain.com");
XmlNodeList nodes = xd.SelectNodes("//xslt:a", nsMgr);

Sorry since i'm new i'm not that fast on this!

thx


Ash said:
Just agreeing with the namespace issue.

I had this problem when first using XML and referred to this document:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexxml/html/xml05202002.asp

Taking your example, making one spelling correction for the closing root
element, adding the necessary additions to support the namespace, and an
extra element to test it I get this:
System.Xml.XmlDocument xd = new XmlDocument();

xd.InnerXml = "<Plaftorm PlatformName=\"empty\" PlatformID=\"1\"
StyleUsage=\"URL\" xmlns:xslt=\"http://www.domain.com\"
PatformProgrammer=\"mynamehere\" DateTime=\"09/12/2003 6:00:54
PM\"> said:
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xd.NameTable);
nsMgr.AddNamespace("xslt","http://www.domain.com");
XmlNodeList nodes = xd.SelectNodes("//xslt:a", nsMgr);
Console.WriteLine(nodes.Count);

Another option would be to remove the namespace from the xml completely:

xd.InnerXml = "<Plaftorm PlatformName=\"empty\" PlatformID=\"1\"
StyleUsage=\"URL\" PatformProgrammer=\"mynamehere\" DateTime=\"09/12/2003
6:00:54 PM\"> said:
XmlNodeList nodes = xd.SelectNodes("//a");
Console.WriteLine(nodes.Count);

hope this helps.
 
Thanks for reply. So, what do you suggest me to do to fix it. Since it's not
my Xml. I have to fix that up just need some lights on it. Thanks in advance

newbie
 
Back
Top