473,508 Members | 2,236 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.WhitespaceHandling=WhitespaceHandling.None;
//xmlDoc.Load(reader);
while (reader.Read())
{

if (reader.NodeType == XmlNodeType.Element)
{

switch (reader.LocalName)
{

//case "ROW":
//string thisC=reader.Value;
//string thisC = reader.ReadElementString();
// builder.Append( getData(reader));
// break;
case "tx_name":
//string thisC=reader.Value;
//string thisC = reader.ReadElementString();
//reader.MoveToContent();
builder.Append(reader.Value.ToString());
break;
}
}

}
//XmlNodeList requestNodeList = xmlDoc.SelectNodes("//request");
//lblXML.Text = requestNodeList.Count.ToString();
//DisplayChildElements(xmlDoc.DocumentElement, false);
if (builder.Length>0)
{
lblXML.Text=builder.ToString();
}
else
{
lblXML.Text="no xml data";
}
}
public string getData(XmlReader reader1)
{
StringBuilder thisPN = new StringBuilder();
while(reader1.Read() )
{
switch (reader1.Name)
{

case "img_name":
thisPN.Append(getImageName(reader1));
break;

}

if (reader1.NodeType==XmlNodeType.Element ||
reader1.NodeType==XmlNodeType.Whitespace)
{
switch (reader1.Name)
{
case "tx_name":
thisPN.Append(reader1.ReadString());
break;
}
}

}
return thisPN.ToString();
}

public string getImageName(XmlReader reader1)
{
StringBuilder thisPN = new StringBuilder();
while(reader1.Read() )
{
if (reader1.NodeType==XmlNodeType.Element ||
reader1.NodeType==XmlNodeType.Whitespace)
{
switch (reader1.Name)
{
case "DATA":
thisPN.Append(reader1.ReadString());
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.com/fmpdsoresult">
<ERRORCODE>0</ERRORCODE>
<DATABASE>CAT_ROCA.FP5</DATABASE>
<LAYOUT />
- <ROW MODID="56" RECORDID="12619892">
<Cat_ID>UK00155</Cat_ID>
<ITEM_id>0604011800</ITEM_id>
<ITEM_ref>801460..4</ITEM_ref>
<orden_grupo>1</orden_grupo>
<tx_name>GIRALDA</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>GIRALDA</tx_ley05>
<tx_ley04>Seat 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_acabados />
<acabados.string_acabados />
<flag_colores>1</flag_colores>
<colors>1|1|1|1||||||||||</colors>
<colors_maxiclean />
<flag_homologaciones />
<homo.string_mosaico />
<print>1|0|0|0</print>
<print_ficha>|0|0|0</print_ficha>
<print_pdf />
</ROW>
- <ROW MODID="77" RECORDID="12621622">
<Cat_ID>rei0311034</Cat_ID>
<ITEM_id>0604011800</ITEM_id>
<ITEM_ref>801462004</ITEM_ref>
<orden_grupo>2</orden_grupo>
<tx_name>GIRALDA</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>Soft 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_tapas.jpg</DATA>
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
</img_name>
<Flag_esquemas>0</Flag_esquemas>
<esq_files>801462004.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_acabados />
<acabados.string_acabados />
<flag_colores>1</flag_colores>
<colors>1|||||||||||||</colors>
<colors_maxiclean />
<flag_homologaciones />
<homo.string_mosaico />
<print>1|0|0|0</print>
<print_ficha>|0|0|0</print_ficha>
<print_pdf />
</ROW>
</FMPDSORESULT>

Nov 12 '05 #1
1 1711
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.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;
class Program {

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

void Run() {
XmlTextReader reader = new
XmlTextReader(("..\\..\\0102020200_rei_uk.xml"));
while (reader.Read()) {
if (reader.NodeType == XmlNodeType.Element) {
switch (reader.LocalName) {
case "ERRORCODE":
break;
case "DATABASE":
break;
case "LAYOUT":
break;
case "ROW":
Row row = Row.ReadRow(reader);
//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_acabados;
string acabados;
string flag_colores;
string colors;
string colors_maxiclean;
string flag_homologaciones;
string homo_string_mosaico;
string print;
string print_ficha;
string print_pdf;

public static Row ReadRow(XmlReader reader) {
Row row = new Row();
while (reader.Read()) {
if (reader.NodeType == XmlNodeType.Element) {
switch (reader.Name) {
case "Cat_ID":
row.catId = reader.ReadString();
break;
case "ITEM_id":
row.itemId = reader.ReadString();
break;
case "ITEM_ref":
row.otemRef = reader.ReadString();
break;
case "orden_grupo":
row.ordenGrupo = reader.ReadString();
break;
case "tx_name":
row.txName = reader.ReadString();
break;
case "tx_desc":
row.txDesc= reader.ReadString();
break;
case "tx_ley01":
row.tx_ley01 = reader.ReadString();
break;
case "tx_ley02":
row.tx_ley02 = reader.ReadString();
break;
case "tx_ley03":
row.tx_ley03 = reader.ReadString();
break;
case "tx_ley04":
row.tx_ley04 = reader.ReadString();
break;
case "tx_ley05":
row.tx_ley05 = reader.ReadString();
break;
case "tx_com01":
row.tx_com01 = reader.ReadString();
break;
case "tx_com02":
row.tx_com02 = reader.ReadString();
break;
case "tx_com03":
row.tx_com03 = reader.ReadString();
break;
case "tx_com04":
row.tx_com04 = reader.ReadString();
break;
case "tx_com05":
row.tx_com05 = reader.ReadString();
break;
case "tx_opc":
row.tx_opc = reader.ReadString();
break;
case "img_name":
row.images = ReadData(reader);
break;
case "Flag_esquemas":
row.flag_esquemas = reader.ReadString();
break;
case "esq_files":
row.esq_files = reader.ReadString();
break;
case "esq_strings":
row.esq_strings = ReadData(reader);
break;
case "flag_acabados":
row.flag_acabados = reader.ReadString();
break;
case "bodegon_acabados":
row.bodegon_acabados = reader.ReadString();
break;
case "acabados.string_acabados":
row.acabados = reader.ReadString();
break;
case "flag_colores":
row.flag_colores = reader.ReadString();
break;
case "colors":
row.colors = reader.ReadString();
break;
case "colors_maxiclean":
row.colors_maxiclean = reader.ReadString();
break;
case "flag_homologaciones":
row.flag_homologaciones = reader.ReadString();
break;
case "homo.string_mosaico":
row.homo_string_mosaico = reader.ReadString();
break;
case "print":
row.print = reader.ReadString();
break;
case "print_ficha":
row.print_ficha = reader.ReadString();
break;
case "print_pdf":
row.print_pdf = reader.ReadString();
break;
}
} else if (reader.NodeType == XmlNodeType.EndElement) {
break;
}
}
return row;
}

static string[] ReadData(XmlReader reader) {
ArrayList list = new ArrayList();
while (reader.Read()) {
if (reader.NodeType == XmlNodeType.Element) {
switch (reader.Name) {
case "DATA":
list.Add(reader.ReadString());
break;

}
} else if (reader.NodeType == XmlNodeType.EndElement) {
break;
}

}
return (string[])list.ToArray(typeof(string));
}

}
"Logician" <sa***@logicians.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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.WhitespaceHandling=WhitespaceHandling.None;
//xmlDoc.Load(reader);
while (reader.Read())
{

if (reader.NodeType == XmlNodeType.Element)
{

switch (reader.LocalName)
{

//case "ROW":
//string thisC=reader.Value;
//string thisC = reader.ReadElementString();
// builder.Append( getData(reader));
// break;
case "tx_name":
//string thisC=reader.Value;
//string thisC = reader.ReadElementString();
//reader.MoveToContent();
builder.Append(reader.Value.ToString());
break;
}
}

}
//XmlNodeList requestNodeList = xmlDoc.SelectNodes("//request");
//lblXML.Text = requestNodeList.Count.ToString();
//DisplayChildElements(xmlDoc.DocumentElement, false);
if (builder.Length>0)
{
lblXML.Text=builder.ToString();
}
else
{
lblXML.Text="no xml data";
}
}
public string getData(XmlReader reader1)
{
StringBuilder thisPN = new StringBuilder();
while(reader1.Read() )
{
switch (reader1.Name)
{

case "img_name":
thisPN.Append(getImageName(reader1));
break;

}

if (reader1.NodeType==XmlNodeType.Element ||
reader1.NodeType==XmlNodeType.Whitespace)
{
switch (reader1.Name)
{
case "tx_name":
thisPN.Append(reader1.ReadString());
break;
}
}

}
return thisPN.ToString();
}

public string getImageName(XmlReader reader1)
{
StringBuilder thisPN = new StringBuilder();
while(reader1.Read() )
{
if (reader1.NodeType==XmlNodeType.Element ||
reader1.NodeType==XmlNodeType.Whitespace)
{
switch (reader1.Name)
{
case "DATA":
thisPN.Append(reader1.ReadString());
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.com/fmpdsoresult">
<ERRORCODE>0</ERRORCODE>
<DATABASE>CAT_ROCA.FP5</DATABASE>
<LAYOUT />
- <ROW MODID="56" RECORDID="12619892">
<Cat_ID>UK00155</Cat_ID>
<ITEM_id>0604011800</ITEM_id>
<ITEM_ref>801460..4</ITEM_ref>
<orden_grupo>1</orden_grupo>
<tx_name>GIRALDA</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>GIRALDA</tx_ley05>
<tx_ley04>Seat 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_acabados />
<acabados.string_acabados />
<flag_colores>1</flag_colores>
<colors>1|1|1|1||||||||||</colors>
<colors_maxiclean />
<flag_homologaciones />
<homo.string_mosaico />
<print>1|0|0|0</print>
<print_ficha>|0|0|0</print_ficha>
<print_pdf />
</ROW>
- <ROW MODID="77" RECORDID="12621622">
<Cat_ID>rei0311034</Cat_ID>
<ITEM_id>0604011800</ITEM_id>
<ITEM_ref>801462004</ITEM_ref>
<orden_grupo>2</orden_grupo>
<tx_name>GIRALDA</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>Soft 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_tapas.jpg</DATA>
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
<DATA />
</img_name>
<Flag_esquemas>0</Flag_esquemas>
<esq_files>801462004.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_acabados />
<acabados.string_acabados />
<flag_colores>1</flag_colores>
<colors>1|||||||||||||</colors>
<colors_maxiclean />
<flag_homologaciones />
<homo.string_mosaico />
<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
1785
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 () ==...
6
1800
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...
6
2393
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. ...
2
7546
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 (...
2
3895
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...
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...
1
4371
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...
0
2422
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...
2
5563
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...
0
7227
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,...
0
7127
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...
0
7331
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7391
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...
1
7054
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...
0
7501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5633
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4713
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...
0
424
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...

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.