473,667 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XMLReader - nodes not found

Task: To Read an XML document and handle data contents in .NET for new
processing

Request: Any details on how to read an XML document processing all
nodes and content in .NET

Problems: Duplicate DATA tags, end nodes (ley01) and then data lower
down, system loses its positions

My Experience: Handling XML/HTML via Transforms, writing XML using
..NET, but not reading

SAmple Code:

XmlTextReader reader = new XmlTextReader(( "0102020200_rei _uk.xml"));
XmlDocument xmlDoc = new XmlDocument();
//reader.Whitespa ceHandling=Whit espaceHandling. None;
//xmlDoc.Load(rea der);
while (reader.Read())
{

if (reader.NodeTyp e == XmlNodeType.Ele ment)
{

switch (reader.LocalNa me)
{

//case "ROW":
//string thisC=reader.Va lue;
//string thisC = reader.ReadElem entString();
// builder.Append( getData(reader) );
// break;
case "tx_name":
//string thisC=reader.Va lue;
//string thisC = reader.ReadElem entString();
//reader.MoveToCo ntent();
builder.Append( reader.Value.To String());
break;
}
}

}
//XmlNodeList requestNodeList = xmlDoc.SelectNo des("//request");
//lblXML.Text = requestNodeList .Count.ToString ();
//DisplayChildEle ments(xmlDoc.Do cumentElement, false);
if (builder.Length >0)
{
lblXML.Text=bui lder.ToString() ;
}
else
{
lblXML.Text="no xml data";
}
}
public string getData(XmlRead er reader1)
{
StringBuilder thisPN = new StringBuilder() ;
while(reader1.R ead() )
{
switch (reader1.Name)
{

case "img_name":
thisPN.Append(g etImageName(rea der1));
break;

}

if (reader1.NodeTy pe==XmlNodeType .Element ||
reader1.NodeTyp e==XmlNodeType. Whitespace)
{
switch (reader1.Name)
{
case "tx_name":
thisPN.Append(r eader1.ReadStri ng());
break;
}
}

}
return thisPN.ToString ();
}

public string getImageName(Xm lReader reader1)
{
StringBuilder thisPN = new StringBuilder() ;
while(reader1.R ead() )
{
if (reader1.NodeTy pe==XmlNodeType .Element ||
reader1.NodeTyp e==XmlNodeType. Whitespace)
{
switch (reader1.Name)
{
case "DATA":
thisPN.Append(r eader1.ReadStri ng());
break;

}
}

}
return thisPN.ToString ();
}


}
}
Sample XML Document - cannot be changed, 500 docs in total:

<?xml version="1.0" encoding="UTF-8" ?>
- <FMPDSORESULT xmlns="http://www.filemaker.c om/fmpdsoresult">
<ERRORCODE>0</ERRORCODE>
<DATABASE>CAT_R OCA.FP5</DATABASE>
<LAYOUT />
- <ROW MODID="56" RECORDID="12619 892">
<Cat_ID>UK00155 </Cat_ID>
<ITEM_id>060401 1800</ITEM_id>
<ITEM_ref>80146 0..4</ITEM_ref>
<orden_grupo> 1</orden_grupo>
<tx_name>GIRALD A</tx_name>
<tx_desc>Seat and cover in lacquered heat-hardened resin, with
stainless steel hinges.</tx_desc>
<tx_ley01 />
<tx_ley02 />
<tx_ley03 />
<tx_ley05>GIRAL DA</tx_ley05>
<tx_ley04>Sea t and cover</tx_ley04>
<tx_com01 />
<tx_com02 />
<tx_com03>In the product number replace (..) with the code for the
chosen colour.</tx_com03>
<tx_com04 />
<tx_com05 />
<tx_opc />
- <img_name>
<DATA>801460004 .jpg</DATA>
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
</img_name>
<Flag_esquemas> 0</Flag_esquemas>
<esq_files />
- <esq_strings>
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
</esq_strings>
<flag_acabados> 0</flag_acabados>
<bodegon_acabad os />
<acabados.strin g_acabados />
<flag_colores>1 </flag_colores>
<colors>1|1|1|1 ||||||||||</colors>
<colors_maxicle an />
<flag_homologac iones />
<homo.string_mo saico />
<print>1|0|0| 0</print>
<print_ficha>|0 |0|0</print_ficha>
<print_pdf />
</ROW>
- <ROW MODID="77" RECORDID="12621 622">
<Cat_ID>rei0311 034</Cat_ID>
<ITEM_id>060401 1800</ITEM_id>
<ITEM_ref>80146 2004</ITEM_ref>
<orden_grupo> 2</orden_grupo>
<tx_name>GIRALD A</tx_name>
<tx_desc>Soft close seat and cover in lacquered heat-hardened resin,
with stainless steel hinges.</tx_desc>
<tx_ley01 />
<tx_ley02 />
<tx_ley03 />
<tx_ley05 />
<tx_ley04>Sof t close seat and cover</tx_ley04>
<tx_com01 />
<tx_com02 />
<tx_com03 />
<tx_com04 />
<tx_com05 />
<tx_opc />
- <img_name>
<DATA>801462xx0 .jpg</DATA>
<DATA>caida_tap as.jpg</DATA>
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
</img_name>
<Flag_esquemas> 0</Flag_esquemas>
<esq_files>8014 62004.swf</esq_files>
- <esq_strings>
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
</esq_strings>
<flag_acabados> 0</flag_acabados>
<bodegon_acabad os />
<acabados.strin g_acabados />
<flag_colores>1 </flag_colores>
<colors>1|||||| |||||||</colors>
<colors_maxicle an />
<flag_homologac iones />
<homo.string_mo saico />
<print>1|0|0| 0</print>
<print_ficha>|0 |0|0</print_ficha>
<print_pdf />
</ROW>
</FMPDSORESULT>

Nov 12 '05 #1
1 1727
Well it depends on the type of processing you need to do. Here's an example
that uses the XmlReader to cache up your row data into an object which you
so you can then process a row at a time.

using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Xml;
using System.Collecti ons;
class Program {

static void Main(string[] args) {
new Program().Run() ;
}

void Run() {
XmlTextReader reader = new
XmlTextReader(( "..\\..\\010202 0200_rei_uk.xml "));
while (reader.Read()) {
if (reader.NodeTyp e == XmlNodeType.Ele ment) {
switch (reader.LocalNa me) {
case "ERRORCODE" :
break;
case "DATABASE":
break;
case "LAYOUT":
break;
case "ROW":
Row row = Row.ReadRow(rea der);
//ProcessRow(row) ;
break;
}
}
}
}
}
class Row {
string catId;
string itemId;
string otemRef;
string ordenGrupo;
string txName;
string txDesc;
string tx_ley01;
string tx_ley02;
string tx_ley03;
string tx_ley04;
string tx_ley05;
string tx_com01;
string tx_com02;
string tx_com03;
string tx_com04;
string tx_com05;
string tx_opc;
string[] images;
string flag_esquemas;
string esq_files;
string[] esq_strings;
string flag_acabados;
string bodegon_acabado s;
string acabados;
string flag_colores;
string colors;
string colors_maxiclea n;
string flag_homologaci ones;
string homo_string_mos aico;
string print;
string print_ficha;
string print_pdf;

public static Row ReadRow(XmlRead er reader) {
Row row = new Row();
while (reader.Read()) {
if (reader.NodeTyp e == XmlNodeType.Ele ment) {
switch (reader.Name) {
case "Cat_ID":
row.catId = reader.ReadStri ng();
break;
case "ITEM_id":
row.itemId = reader.ReadStri ng();
break;
case "ITEM_ref":
row.otemRef = reader.ReadStri ng();
break;
case "orden_grup o":
row.ordenGrupo = reader.ReadStri ng();
break;
case "tx_name":
row.txName = reader.ReadStri ng();
break;
case "tx_desc":
row.txDesc= reader.ReadStri ng();
break;
case "tx_ley01":
row.tx_ley01 = reader.ReadStri ng();
break;
case "tx_ley02":
row.tx_ley02 = reader.ReadStri ng();
break;
case "tx_ley03":
row.tx_ley03 = reader.ReadStri ng();
break;
case "tx_ley04":
row.tx_ley04 = reader.ReadStri ng();
break;
case "tx_ley05":
row.tx_ley05 = reader.ReadStri ng();
break;
case "tx_com01":
row.tx_com01 = reader.ReadStri ng();
break;
case "tx_com02":
row.tx_com02 = reader.ReadStri ng();
break;
case "tx_com03":
row.tx_com03 = reader.ReadStri ng();
break;
case "tx_com04":
row.tx_com04 = reader.ReadStri ng();
break;
case "tx_com05":
row.tx_com05 = reader.ReadStri ng();
break;
case "tx_opc":
row.tx_opc = reader.ReadStri ng();
break;
case "img_name":
row.images = ReadData(reader );
break;
case "Flag_esquemas" :
row.flag_esquem as = reader.ReadStri ng();
break;
case "esq_files" :
row.esq_files = reader.ReadStri ng();
break;
case "esq_string s":
row.esq_strings = ReadData(reader );
break;
case "flag_acabados" :
row.flag_acabad os = reader.ReadStri ng();
break;
case "bodegon_acabad os":
row.bodegon_aca bados = reader.ReadStri ng();
break;
case "acabados.strin g_acabados":
row.acabados = reader.ReadStri ng();
break;
case "flag_color es":
row.flag_colore s = reader.ReadStri ng();
break;
case "colors":
row.colors = reader.ReadStri ng();
break;
case "colors_maxicle an":
row.colors_maxi clean = reader.ReadStri ng();
break;
case "flag_homologac iones":
row.flag_homolo gaciones = reader.ReadStri ng();
break;
case "homo.string_mo saico":
row.homo_string _mosaico = reader.ReadStri ng();
break;
case "print":
row.print = reader.ReadStri ng();
break;
case "print_fich a":
row.print_ficha = reader.ReadStri ng();
break;
case "print_pdf" :
row.print_pdf = reader.ReadStri ng();
break;
}
} else if (reader.NodeTyp e == XmlNodeType.End Element) {
break;
}
}
return row;
}

static string[] ReadData(XmlRea der reader) {
ArrayList list = new ArrayList();
while (reader.Read()) {
if (reader.NodeTyp e == XmlNodeType.Ele ment) {
switch (reader.Name) {
case "DATA":
list.Add(reader .ReadString());
break;

}
} else if (reader.NodeTyp e == XmlNodeType.End Element) {
break;
}

}
return (string[])list.ToArray(t ypeof(string));
}

}
"Logician" <sa***@logician s.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Task: To Read an XML document and handle data contents in .NET for new
processing

Request: Any details on how to read an XML document processing all
nodes and content in .NET

Problems: Duplicate DATA tags, end nodes (ley01) and then data lower
down, system loses its positions

My Experience: Handling XML/HTML via Transforms, writing XML using
.NET, but not reading

SAmple Code:

XmlTextReader reader = new XmlTextReader(( "0102020200_rei _uk.xml"));
XmlDocument xmlDoc = new XmlDocument();
//reader.Whitespa ceHandling=Whit espaceHandling. None;
//xmlDoc.Load(rea der);
while (reader.Read())
{

if (reader.NodeTyp e == XmlNodeType.Ele ment)
{

switch (reader.LocalNa me)
{

//case "ROW":
//string thisC=reader.Va lue;
//string thisC = reader.ReadElem entString();
// builder.Append( getData(reader) );
// break;
case "tx_name":
//string thisC=reader.Va lue;
//string thisC = reader.ReadElem entString();
//reader.MoveToCo ntent();
builder.Append( reader.Value.To String());
break;
}
}

}
//XmlNodeList requestNodeList = xmlDoc.SelectNo des("//request");
//lblXML.Text = requestNodeList .Count.ToString ();
//DisplayChildEle ments(xmlDoc.Do cumentElement, false);
if (builder.Length >0)
{
lblXML.Text=bui lder.ToString() ;
}
else
{
lblXML.Text="no xml data";
}
}
public string getData(XmlRead er reader1)
{
StringBuilder thisPN = new StringBuilder() ;
while(reader1.R ead() )
{
switch (reader1.Name)
{

case "img_name":
thisPN.Append(g etImageName(rea der1));
break;

}

if (reader1.NodeTy pe==XmlNodeType .Element ||
reader1.NodeTyp e==XmlNodeType. Whitespace)
{
switch (reader1.Name)
{
case "tx_name":
thisPN.Append(r eader1.ReadStri ng());
break;
}
}

}
return thisPN.ToString ();
}

public string getImageName(Xm lReader reader1)
{
StringBuilder thisPN = new StringBuilder() ;
while(reader1.R ead() )
{
if (reader1.NodeTy pe==XmlNodeType .Element ||
reader1.NodeTyp e==XmlNodeType. Whitespace)
{
switch (reader1.Name)
{
case "DATA":
thisPN.Append(r eader1.ReadStri ng());
break;

}
}

}
return thisPN.ToString ();
}


}
}
Sample XML Document - cannot be changed, 500 docs in total:

<?xml version="1.0" encoding="UTF-8" ?>
- <FMPDSORESULT xmlns="http://www.filemaker.c om/fmpdsoresult">
<ERRORCODE>0</ERRORCODE>
<DATABASE>CAT_R OCA.FP5</DATABASE>
<LAYOUT />
- <ROW MODID="56" RECORDID="12619 892">
<Cat_ID>UK00155 </Cat_ID>
<ITEM_id>060401 1800</ITEM_id>
<ITEM_ref>80146 0..4</ITEM_ref>
<orden_grupo> 1</orden_grupo>
<tx_name>GIRALD A</tx_name>
<tx_desc>Seat and cover in lacquered heat-hardened resin, with
stainless steel hinges.</tx_desc>
<tx_ley01 />
<tx_ley02 />
<tx_ley03 />
<tx_ley05>GIRAL DA</tx_ley05>
<tx_ley04>Sea t and cover</tx_ley04>
<tx_com01 />
<tx_com02 />
<tx_com03>In the product number replace (..) with the code for the
chosen colour.</tx_com03>
<tx_com04 />
<tx_com05 />
<tx_opc />
- <img_name>
<DATA>801460004 .jpg</DATA>
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
</img_name>
<Flag_esquemas> 0</Flag_esquemas>
<esq_files />
- <esq_strings>
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
</esq_strings>
<flag_acabados> 0</flag_acabados>
<bodegon_acabad os />
<acabados.strin g_acabados />
<flag_colores>1 </flag_colores>
<colors>1|1|1|1 ||||||||||</colors>
<colors_maxicle an />
<flag_homologac iones />
<homo.string_mo saico />
<print>1|0|0| 0</print>
<print_ficha>|0 |0|0</print_ficha>
<print_pdf />
</ROW>
- <ROW MODID="77" RECORDID="12621 622">
<Cat_ID>rei0311 034</Cat_ID>
<ITEM_id>060401 1800</ITEM_id>
<ITEM_ref>80146 2004</ITEM_ref>
<orden_grupo> 2</orden_grupo>
<tx_name>GIRALD A</tx_name>
<tx_desc>Soft close seat and cover in lacquered heat-hardened resin,
with stainless steel hinges.</tx_desc>
<tx_ley01 />
<tx_ley02 />
<tx_ley03 />
<tx_ley05 />
<tx_ley04>Sof t close seat and cover</tx_ley04>
<tx_com01 />
<tx_com02 />
<tx_com03 />
<tx_com04 />
<tx_com05 />
<tx_opc />
- <img_name>
<DATA>801462xx0 .jpg</DATA>
<DATA>caida_tap as.jpg</DATA>
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
</img_name>
<Flag_esquemas> 0</Flag_esquemas>
<esq_files>8014 62004.swf</esq_files>
- <esq_strings>
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
</esq_strings>
<flag_acabados> 0</flag_acabados>
<bodegon_acabad os />
<acabados.strin g_acabados />
<flag_colores>1 </flag_colores>
<colors>1|||||| |||||||</colors>
<colors_maxicle an />
<flag_homologac iones />
<homo.string_mo saico />
<print>1|0|0| 0</print>
<print_ficha>|0 |0|0</print_ficha>
<print_pdf />
</ROW>
</FMPDSORESULT>

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1796
by: Philipp Schumann | last post by:
Hi, I have the following code to copy nodes from an XML document (XmlReader reader) to some output (XmlWriter writer). while (reader.Read ()) if (reader.MoveToContent () == XmlNodeType.Element) break; // forward the reader to the document element writer.WriteStartDocument (false);
6
1814
by: Michael | last post by:
Hi All, I need a few suggestions. I have the following XML segment: <LookUp> <ControlType>CheckBoxGroup</ControlType> <DBField>LastMedDate</DBField> <ControlName>cmbGoal1</ControlName> <Values VALUE="0" BookMark="Goal1Progress"/> <Values VALUE="1" BookMark="Goal1NoProgress"/> <Values VALUE="2" BookMark="Goal1NA"/> </LookUp>
6
2399
by: Rob Meade | last post by:
Hi all, I'm having a few difficulties with the above, ie I cant find any good examples anywhere of how to do what I want to do! I have an xml file on my desktop which I want to read in. Having checked the info for XML file reading it suggests that with .net 2.0 you should use the XMLReader class...
2
7576
by: raj | last post by:
i have this code which is upgraded to work with 2.0 and works great with framework 1.0 and 1.1 without the upgraded code. //2.0 code// public static XmlReader RtfToXml( string Rtf ) { if ( moForm == null )
2
3901
by: Amit | last post by:
I have assigned a simple xml into a XmlReader. A switch case loops through the nodes and writes the data to XmlWriter object which is initialized to a xml file. The output xml file is correctly generated and everything works fine. But if the input xml has an &amp; then I am unable to generate a valid output xml file. I have pasted the code below: public class Class1 {
6
2142
by: Mitch | last post by:
Hello, I have a singleton class that loads a config file (for an assembly) into an internal stream object. I designed this class as singleton because it parses the config file many times and I do not want to load the config file everytime. The problem is I am using XMLReader because of speed and after I read an item (multiple nodes so pass node name and element name) and try to read another item I get an error "root element missing". ...
1
4378
by: dignan.tenenbaum | last post by:
Hello, I'm using the XmlReader.ReadOuterXml() method to return the string representation of an xml node. The XmlReader is created with a file path and a XmlReaderSettings object. This was working fine until a default namespace was introduced. When the default namespace is defined the ReadOuterXml() method began adding a bunch of extra namespace declarations to the string it returned. xml example ...
0
2445
by: gpet44 | last post by:
Hi, I have a problem receiving XML over a NetworkStream in C# .Net 2.0/3.0. I'm creating an XMLReader from the stream. I get the following exception when the XmlReaderreads from the stream as follows while(reader.Read()) { ... } System.Xml.XmlException: Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 2, position 3. Unfortunately I...
2
5584
by: SammyBar | last post by:
Hi all, I'm trying to convert the xml obtained from a XmlReader object into a UTF-8 array. My general idea is to read the XmlReader and write into a MemoryStream. Then convert the MemoryStream bytes into utf-8. MemoryStream ms = new MemoryStream(); XmlTextWriter xmlWriter = new XmlTextWriter(ms, new UTF8Encoding(false)); writer.Formatting = Formatting.Indented;
0
8458
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8366
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8565
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1779
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.