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>