Groups | Blog | Home
all groups > dotnet xml > august 2005 >

dotnet xml : XMLReader - nodes not found


Logician
8/14/2005 12:45:53 AM
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>
Chris Lovett
8/14/2005 5:48:09 PM
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;
AddThis Social Bookmark Button