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>