Cdata, C# and XML - CSharp
This is a discussion on Cdata, C# and XML - CSharp ; Does someone have a link for reading CDATA in C#?
I have the problem that the ReadString method fails. I have tried a
few things but the data is not read correctly.
I would appreciate any help....
-
Cdata, C# and XML
Does someone have a link for reading CDATA in C#?
I have the problem that the ReadString method fails. I have tried a
few things but the data is not read correctly.
I would appreciate any help.
-
Re: Cdata, C# and XML
Logician <sales@logicians.com> wrote:
> Does someone have a link for reading CDATA in C#?
>
> I have the problem that the ReadString method fails. I have tried a
> few things but the data is not read correctly.
>
> I would appreciate any help.
Could you post a short but complete program which demonstrates the
problem?
See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
-
Re: Cdata, C# and XML
On Jan 28, 8:19 pm, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:
> Logician <sa...@logicians.com> wrote:
> > Does someone have a link for reading CDATA in C#?
>
> > I have the problem that the ReadString method fails. I have tried a
> > few things but the data is not read correctly.
>
> > I would appreciate any help.
>
> Could you post a short but complete program which demonstrates the
> problem?
>
> Seehttp://www.pobox.com/~skeet/csharp/complete.htmlfor details of
> what I mean by that.
>
> --
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk
Hi Jon
I meant to post the code but I was at home without it. The C# program
is dead simple and just reads the nodes, and it works fine except for
the CDATA. It did work and then suddenly stopped, so I probably
accidently changed it (the original code was two years old). So I am
sure it is a small change needed. The XML file is a data file of
bathroom data and has basic descriptions, images, and comments. There
are about a 1500 files to read and handle, so a program is needed.
I have searched google for relevant data on this and found various
combinations but nothing is really working for me. It is something
related to reading strings, values, attributes. I read several posts
of people having problems in parsing CDATA but the solutions seemed to
be use Value which does not work for me.
The key problems are with farbname (colour in German), kurztext (short
text), and langtext. I decided not to navigate the parent structure
and it seemed to make no difference, and I just used the nodenames.
Here is the code running under .NET 1.1, C#, Windows Forms (under
Windows XP):
<?xml version="1.0" encoding="UTF-16" ?>
- <produkt>
<produktid value="6858651" />
<artnr value="33304" />
<kombipr value="false" />
- <kurztext>
- <![CDATA[ Single-lever bath/shower mixer 1/2"
]]>
</kurztext>
<gewusst_wie value="false" />
- <internettext>
- <![CDATA[
]]>
</internettext>
- <stlb>
- <![CDATA[
]]>
</stlb>
<pid value="6858657" />
<dxf value="/dxf/1074194.zip" />
- <langtext>
- <![CDATA[ metal lever<br>46 mm ceramic cartridge with <br>Eco-
Override-Stop, with <b>GROHE SilkMoveŽ</b><br>adjustable flow rate
limiter<br>adjustable minimum flow rate 2.5 l/min<br>automatic
diverter: bath/shower<br>integrated non-return valves <br>in the
shower outlet 1/2"<br>mousseur aerator (HP)<br>without connection
unions<br>optional temperature limiter<br>protected against
backflow<br>Min. recommended pressure 1.0 bar<br>Balanced hot and cold
supplies
]]>
</langtext>
<mbild value="./images/draw/Z33304001.jpg" online="./images/draw/
online/Z33304001.jpg" width="36" height="60" />
<stbild value="./images/strich/S33304001.gif" online="./images/
strich/online/S33304001.jpg" width="87" height="60" />
<swbild value="./images/sw/F33304001.gif" online="./images/sw/online/
F33304001.jpg" thumbnails="./images/sw/thumbnails/F33304001.jpg" />
<gruppe value="02" />
<designlinie value="05" />
<prodtyp value="05" />
<sge value="02" />
<montage value="01" />
<zapfstelle value="03" />
<linie value="D67" />
<design value="67" />
<brausen value="01" />
- <produktliniename>
- <![CDATA[ PL_Eurosmart
]]>
</produktliniename>
<produktlinienid value="6850046" />
- <produktlinientext>
- <![CDATA[ Eurosmart
]]>
</produktlinientext>
<ranking value="7" />
<pq value="./images/pq/EnglischUK/D33304.gif" online="./images/pq/
online/EnglischUK/D33304.jpg" />
- <farben>
- <farbe>
<produktid value="6858651" />
- <farbname>
- <![CDATA[ chrome
]]>
</farbname>
<schluessel value="001" />
<vvs value="" />
<pbild value="./images/prod/33304001.gif" online="./images/prod/
online/33304001.jpg" thumbnails="./images/prod/thumbnails/
33304001.jpg" />
<ranking value="0" />
<stueckliste value="true" />
<explozeichnung value="./images/explo/E33304001.jpg" online="./
images/explo/online/E33304001.jpg" width="44" height="60" />
<preis value="90.33" />
<preis2 value="" />
</farbe>
</farben>
</produkt>
private void handleGroheXML(string filePath1)
{
// write file
string thisOutFilePath = "c:\\grohe\\xml\\processed.txt";
FileStream fs = new
FileStream(thisOutFilePath,FileMode.Append,FileAccess.Write,FileShare.Write);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(filePath1);
sw.Close();
// end write file
string thisXMLName="";
filePath1="C:\\grohe\\xml\\"+filePath1;
XmlTextReader reader = new XmlTextReader((filePath1));
reader.WhitespaceHandling=WhitespaceHandling.None;
XmlDocument xmlDoc = new XmlDocument();
int index1 = filePath1.IndexOf(".xml");
if (index1>0)
{
while(reader.Read())
{
//if (reader.NodeType == XmlNodeType.Element)
if (reader.LocalName.Length>0)
{
//////////Trace.Warn("reader.localname top =="+
reader.LocalName);
string thisNodeType = reader.NodeType.ToString();
thisXMLName = reader.LocalName;
if (thisXMLName == "farbname")
{
string xx="";
thisColour = reader.Value;
//string jj1 = reader.LocalName;
string jj="";
thisColour = reader.ReadString();
//thisColour=reader.GetAttribute("CDATA");
thisPOID = handleColour(thisColour);
}
if (thisXMLName == "produktid")
{
string hh1="";
}
if (thisXMLName == "artnr")
{
thisPartNumber = reader.GetAttribute("value");
thisManPartNumber = thisPartNumber;
//string thisNodeType = reader.NodeType.ToString();
//ReadAttributeValue("value");
//string gg="";
}
if (thisXMLName == "kurztext")
{
thisShortDesc = reader.ReadString();
thisShortDesc=thisShortDesc.Replace("<b>","");
thisShortDesc=thisShortDesc.Replace("</b>","");
thisShortDesc=thisShortDesc.Replace("<Normal>","");
thisShortDesc=thisShortDesc.Replace("</Normal>","");
// trevor
thisPartDesc = thisShortDesc;
if (thisShortDesc.Length>50)
{
thisShortDesc = thisShortDesc.Substring(0,50);
}
//string thisNodeType = reader.NodeType.ToString();
//ReadAttributeValue("value");
}
if (thisXMLName == "gruppe")
{
thisFamily = reader.GetAttribute("value");
//string thisNodeType = reader.NodeType.ToString();
//ReadAttributeValue("value");
}
if (thisXMLName == "langtext")
{
thisCommentStr = reader.ReadString();
//string thisNodeType = reader.NodeType.ToString();
//ReadAttributeValue("value");
}
if (thisXMLName == "pbild")
{
// process pbild
}
// get schusell prefix
if (thisXMLName == "mbild")
{
thisSizeImage = reader.GetAttribute("value");
thisSizeImage=thisSizeImage.Replace("./images/draw/","");
}
if (thisXMLName == "stbild")
{
thisLineImage = reader.GetAttribute("value");
thisLineImage=thisLineImage.Replace("./images/strich/","");
}
if (thisXMLName == "swbild")
{
thisOrgPic = reader.GetAttribute("value");
thisOrgPic=thisOrgPic.Replace("./images/sw/","");
}
if (thisXMLName == "explozeichnung")
{
thisInstallationPic = reader.GetAttribute("value");
thisInstallationPic=thisInstallationPic.Replace("./images/
explo/","");
}
if (thisXMLName == "pq")
{
thisFlowRatePic = reader.GetAttribute("value");
thisFlowRatePic=thisFlowRatePic.Replace("./images/pq/","");
thisFlowRatePic=thisFlowRatePic.Replace("./images/prod/","");
}
if (thisXMLName == "produktliniename")
{
thisRange = reader.ReadString();
}
if (thisXMLName == "schluessel")
{
thisPartNumberOption= reader.GetAttribute("value");
}
if (thisXMLName == "preis")
{
string thisPriceS= reader.GetAttribute("value");
// process price
//if (thisXMLName == "farben")
//{
// get farbe record
// reader.ReadStartElement("farbname");
//
// string thisData = reader.ReadString();
//
// reader.ReadEndElement();
//}
}
}
}
}
}
-
Re: Cdata, C# and XML
Logician <sales@logicians.com> wrote:
> I meant to post the code but I was at home without it. The C# program
> is dead simple and just reads the nodes, and it works fine except for
> the CDATA. It did work and then suddenly stopped, so I probably
> accidently changed it (the original code was two years old). So I am
> sure it is a small change needed.
I dare say it is, but it's pretty hard to know what the change is
without seeing the code. Please post a short but complete program
(there's no need for hundreds of lines of business-specific code) and
XML file (in true XML, not pasted from IE) so we can reproduce the
problem and fix it.
--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
-
Re: Cdata, C# and XML
This is sample VB6 code which I wrote to retrieve CDATA node in xml
document using DOM. I am sure there should be something similar
Dim s As String
s = "<a><![CDATA[hello world whats up?]]></a>"
Dim x As New DOMDocument, c As IXMLDOMCDATASection
MsgBox x.loadXML(s)
Set c = x.selectSingleNode("//a").childNodes(0)
Hope this helps
Kalpesh
-
Re: Cdata, C# and XML
Kalpesh Shah <shahkalpesh@gmail.com> wrote:
> This is sample VB6 code which I wrote to retrieve CDATA node in xml
> document using DOM. I am sure there should be something similar
>
> Dim s As String
> s = "<a><![CDATA[hello world whats up?]]></a>"
>
> Dim x As New DOMDocument, c As IXMLDOMCDATASection
> MsgBox x.loadXML(s)
>
> Set c = x.selectSingleNode("//a").childNodes(0)
>
>
> Hope this helps
See http://pobox.com/~skeet/csharp/incomplete.html
I hadn't even thinking of putting the question of "Is it in the right
language?" on the list before now.
--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
-
Re: Cdata, C# and XML

I am sorry about putting example in VB6 (as against c#)
Just what I found quick way to answer the problem using VBA.
Thanks
Kalpesh
-
Re: Cdata, C# and XML
On Jan 29, 7:21 am, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:
> Logician <sa...@logicians.com> wrote:
> > I meant to post the code but I was at home without it. The C# program
> > is dead simple and just reads the nodes, and it works fine except for
> > the CDATA. It did work and then suddenly stopped, so I probably
> > accidently changed it (the original code was two years old). So I am
> > sure it is a small change needed.
>
> I dare say it is, but it's pretty hard to know what the change is
> without seeing the code. Please post a short but complete program
> (there's no need for hundreds of lines of business-specific code) and
> XML file (in true XML, not pasted from IE) so we can reproduce the
> problem and fix it.
>
> --
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk
Jon, I have copied pasted some simple code and the xml file below. The
code just reads the xml file in a loop and then tests the node names.
This whole issue is one of the c#/.NET parsing the XML and identifying
the nodes correctly. I have seen some posts suggesting there is a
problem in this matter with c# and XML. The XML file is a real
business file as supplied by the manufacturer of the product line.
The C# debugger clearly shows the data held in the reader object under
CDATAToken and in the Value field but the Value method does not get
the data. It just gets null when used. I have tried other methods like
ReadString and they all fail too when reading CDATA but otherwise they
work on the nodes.
The XML file seems to be valid, so the issue is a c# language one.
If you know how to read CDATA using c# then I would appreciate you
telling me.
<?xml version="1.0" encoding="UTF-16" ?><produkt><produktid
value="6027726" /><artnr value="34224" /><kombipr value="false" /
><kurztext><![CDATA[Dual control shower mixer 1/2" ]]></
kurztext><gewusst_wie value="false"/><internettext><![CDATA[]]></
internettext><stlb><![CDATA[]]></stlb><pid value="6850218" /><dxf
value="" /><langtext><![CDATA[Thermostatic<br>suitable for high / low
pressure<br>concealed brass body & trim set <br>flow &
temperature control handles<br>wax thermoelement<br>installation
bracket for body<br>built-in non return valve<br>dirt
strainers<br>shower outlet 1/2" top or bottom<br>flow limiters for
high (>1.0bar) or unequal pressures<br>recommended pressure 0.1 -
5.0 bar]]></langtext><mbild value="./images/draw/Z3422401.jpg"
online="./images/draw/online/Z3422401.jpg" width="36" height="60"/
><stbild value="./images/strich/S3422401.gif" online="./images/strich/
online/S3422401.jpg" width="49" height="60"/><swbild value="./images/
sw/ZZG_34224000_V01_01.gif" online="./images/sw/online/
ZZG_34224000_V01_01.jpg" thumbnails="./images/sw/thumbnails/
ZZG_34224000_V01_01.jpg"/><gruppe value="XX" /><designlinie
value="XX" /><prodtyp value="XX" /><sge value="XX" /><montage
value="XX" /><zapfstelle value="XX" /><linie value="LXX" /><design
value="XX" /><brausen value="XX" /><produktliniename><!
[CDATA[PL_Avensys]]></produktliniename><produktlinienid
value="6850209" /><produktlinientext><![CDATA[Avensys Modern]]></
produktlinientext><ranking value="3" /><pq value="./images/pq/
EnglischUK/D34224.gif" online="./images/pq/online/EnglischUK/
D34224.jpg" /><farben><farbe><produktid value="6027726" /><farbname><!
[CDATA[chrome]]></farbname><schluessel value="000"/><vvs value="" /
><pbild value="./images/prod/ZZF_34224000_V01_01.gif" online="./images/
prod/online/ZZF_34224000_V01_01.jpg" thumbnails="./images/prod/
thumbnails/ZZF_34224000_V01_01.jpg"/><ranking value="0"/><stueckliste
value="true"/><explozeichnung value="./images/explo/E3422401.jpg"
online="./images/explo/online/E3422401.jpg" width="92" height="60"/
><preis value="277.49"/><preis2 value=""/></farbe></farben></produkt>
private void sampleXML()
{
string filePath1 = "samplexml.xml";
string thisXMLName="";
filePath1="C:\\grohe\\xml\\"+filePath1;
XmlTextReader reader = new XmlTextReader((filePath1));
reader.WhitespaceHandling=WhitespaceHandling.None;
XmlDocument xmlDoc = new XmlDocument();
int index1 = filePath1.IndexOf(".xml");
if (index1>0)
{
while(reader.Read())
{
//if (reader.NodeType == XmlNodeType.Element)
if (reader.LocalName.Length>0)
{
//////////Trace.Warn("reader.localname top =="+
reader.LocalName);
string thisNodeType = reader.NodeType.ToString();
thisXMLName = reader.LocalName;
if (thisXMLName == "farbname")
{
string xx="";
string thisColour = reader.Value;
//string jj1 = reader.LocalName;
string jj="";
thisColour = reader.ReadString();
string gg2 = reader.NodeType.ToString();
//thisColour=reader.GetAttribute("CDATA");
}
if (thisXMLName == "produktid")
{
string hh1="";
}
if (thisXMLName == "artnr")
{
string thisPartNumber = reader.GetAttribute("value");
}
if (thisXMLName == "kurztext")
{
string thisShortDesc = reader.ReadString();
}
if (thisXMLName == "gruppe")
{
string thisFamily = reader.GetAttribute("value");
}
if (thisXMLName == "langtext")
{
string thisCommentStr = reader.ReadString();
}
if (thisXMLName == "pbild")
{
string thisPicName = reader.GetAttribute("value");
}
// get schusell prefix
if (thisXMLName == "mbild")
{
string thisSizeImage = reader.GetAttribute("value");
}
if (thisXMLName == "stbild")
{
string thisLineImage = reader.GetAttribute("value");
}
if (thisXMLName == "swbild")
{
string thisOrgPic = reader.GetAttribute("value");
}
if (thisXMLName == "explozeichnung")
{
string thisInstallationPic = reader.GetAttribute("value");
}
if (thisXMLName == "pq")
{
string thisFlowRatePic = reader.GetAttribute("value");
}
if (thisXMLName == "produktliniename")
{
string thisRange = reader.ReadString();
}
if (thisXMLName == "schluessel")
{
string thisPartNumberOption= reader.GetAttribute("value");
}
if (thisXMLName == "preis")
{
string thisPriceS= reader.GetAttribute("value");
}
}
}
}
}
-
Re: Cdata, C# and XML
On Jan 29, 4:33 pm, Kalpesh Shah <shahkalp...@gmail.com> wrote:
> This is sample VB6 code which I wrote to retrieve CDATA node in xml
> document using DOM. I am sure there should be something similar
>
> Dim s As String
> s = "<a><![CDATA[hello world whats up?]]></a>"
>
> Dim x As New DOMDocument, c As IXMLDOMCDATASection
> MsgBox x.loadXML(s)
>
> Set c = x.selectSingleNode("//a").childNodes(0)
>
> Hope this helps
> Kalpesh
I only use c#. You state that CDATA is a node, but I think it is not a
node. Microsoft calls CDATA a section. This difference is the problem
with reading the data in c#.
See http://msdn2.microsoft.com/en-us/library/ms256076.aspx for more on
CDATA. CDATA was used as the data has HTML in it (eg <br>). The node
names are langtext for example, which the c# parser can identify. The
only problem is therefore extracting the data from what is held in
object. That just needs the right method.
-
Re: Cdata, C# and XML
Logician <sales@logicians.com> wrote:
<snip>
> Jon, I have copied pasted some simple code and the xml file below.
Well, the code was neither short but complete, but never mind...
You're calling XmlReader.ReadString() but then ignoring the result. If
you look at the value of thisColour in your farbname block (the second
value you assign, not the first) you'll find it's "chrome" which I
assume is what you wanted.
--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk