EDN Admin
Well-known member
Hi All,
Here i am iterating the xml schema to find value of TotalRecordCount, is any other way to simplify my code,
<span style="font-family:"Courier New"; color:blue; font-size:10pt
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; namespace XmlDomDemo3
{
<span style="color:Blue; class Program
{
<span style="color:Blue; static <span style="color:Blue; void Main(<span style="color:Blue; string[] args)
{
XmlDocument dom = <span style="color:Blue; new XmlDocument();
dom.Load(<span style="color:#A31515; @"c:test.xml");
XmlElement root = dom.DocumentElement;
XmlNode node = root.FirstChild;
<span style="color:Blue; int TotalRecords = 0;
<span style="color:Blue; bool find = <span style="color:Blue; false;
<span style="color:Blue; while (node.HasChildNodes)
{
<span style="color:Blue; foreach (XmlNode childNode <span style="color:Blue; in node.ChildNodes)
{
<span style="color:Blue; foreach (XmlAttribute childAttribute <span style="color:Blue; in childNode.Attributes)
{
<span style="color:Blue; if (childAttribute.Name == <span style="color:#A31515; "msdata:TotalRecordCount")
{
TotalRecords = Convert.ToInt32(childAttribute.InnerText);
find = <span style="color:Blue; true;
<span style="color:Blue; break;
}
}
<span style="color:Blue; if (find)
<span style="color:Blue; break;
<span style="color:Blue; else
node = childNode;
}
<span style="color:Blue; if (find)
<span style="color:Blue; break;
}
Console.WriteLine(<span style="color:Blue; string.Format(<span style="color:#A31515; "Total Records:{0}",TotalRecords));
Console.ReadLine();
}
}
}
[/code]
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <?<span style="color:#A31515; xml <span style="color:Red; version<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1.0<span style="color:Black; " <span style="color:Red; standalone<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; yes<span style="color:Black; "<span style="color:Blue; ?>
<span style="color:Blue; <<span style="color:#A31515; NewDataSet<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; schema <span style="color:Red; id<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; NewDataSet<span style="color:Black; " <span style="color:Red; xmlns<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; " xmlns:xs="http://www.w3.org/2001/XMLSchema<span style="color:Black; " <span style="color:Red; xmlns:msdata<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; urn:schemas-microsoft-com:xml-msdata<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; NewDataSet<span style="color:Black; " <span style="color:Red; msdata:IsDataSet<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; true<span style="color:Black; " <span style="color:Red; msdata:MainDataTable<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Test<span style="color:Black; " <span style="color:Red; msdata:UseCurrentLocale<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; true<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; complexType<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; choice <span style="color:Red; minOccurs<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 0<span style="color:Black; " <span style="color:Red; maxOccurs<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; unbounded<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Test<span style="color:Black; " <span style="color:Red; msdata:TotalRecordCount<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 5000<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; complexType<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; sequence<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Id<span style="color:Black; " <span style="color:Red; type<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; xs:int<span style="color:Black; " <span style="color:Red; minOccurs<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 0<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Name<span style="color:Black; " <span style="color:Red; type<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; xs:string<span style="color:Black; " <span style="color:Red; minOccurs<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 0<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Price<span style="color:Black; " <span style="color:Red; type<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; xs:double<span style="color:Black; " <span style="color:Red; minOccurs<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 0<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; sequence<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; complexType<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; choice<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; complexType<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; schema<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Test<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Id<span style="color:Blue; >1000<span style="color:Blue; </<span style="color:#A31515; Id<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Name<span style="color:Blue; >Laptop<span style="color:Blue; </<span style="color:#A31515; Name<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Price<span style="color:Blue; >40000<span style="color:Blue; </<span style="color:#A31515; Price<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; Test<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Test<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Id<span style="color:Blue; >1001<span style="color:Blue; </<span style="color:#A31515; Id<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Name<span style="color:Blue; >Monitor<span style="color:Blue; </<span style="color:#A31515; Name<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Price<span style="color:Blue; >5000<span style="color:Blue; </<span style="color:#A31515; Price<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; Test<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Test<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Id<span style="color:Blue; >1000<span style="color:Blue; </<span style="color:#A31515; Id<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Name<span style="color:Blue; >Mouse<span style="color:Blue; </<span style="color:#A31515; Name<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Price<span style="color:Blue; >200<span style="color:Blue; </<span style="color:#A31515; Price<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; Test<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; NewDataSet<span style="color:Blue; >
[/code]
View the full article
Here i am iterating the xml schema to find value of TotalRecordCount, is any other way to simplify my code,
<span style="font-family:"Courier New"; color:blue; font-size:10pt
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; namespace XmlDomDemo3
{
<span style="color:Blue; class Program
{
<span style="color:Blue; static <span style="color:Blue; void Main(<span style="color:Blue; string[] args)
{
XmlDocument dom = <span style="color:Blue; new XmlDocument();
dom.Load(<span style="color:#A31515; @"c:test.xml");
XmlElement root = dom.DocumentElement;
XmlNode node = root.FirstChild;
<span style="color:Blue; int TotalRecords = 0;
<span style="color:Blue; bool find = <span style="color:Blue; false;
<span style="color:Blue; while (node.HasChildNodes)
{
<span style="color:Blue; foreach (XmlNode childNode <span style="color:Blue; in node.ChildNodes)
{
<span style="color:Blue; foreach (XmlAttribute childAttribute <span style="color:Blue; in childNode.Attributes)
{
<span style="color:Blue; if (childAttribute.Name == <span style="color:#A31515; "msdata:TotalRecordCount")
{
TotalRecords = Convert.ToInt32(childAttribute.InnerText);
find = <span style="color:Blue; true;
<span style="color:Blue; break;
}
}
<span style="color:Blue; if (find)
<span style="color:Blue; break;
<span style="color:Blue; else
node = childNode;
}
<span style="color:Blue; if (find)
<span style="color:Blue; break;
}
Console.WriteLine(<span style="color:Blue; string.Format(<span style="color:#A31515; "Total Records:{0}",TotalRecords));
Console.ReadLine();
}
}
}
[/code]
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <?<span style="color:#A31515; xml <span style="color:Red; version<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1.0<span style="color:Black; " <span style="color:Red; standalone<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; yes<span style="color:Black; "<span style="color:Blue; ?>
<span style="color:Blue; <<span style="color:#A31515; NewDataSet<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; schema <span style="color:Red; id<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; NewDataSet<span style="color:Black; " <span style="color:Red; xmlns<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; " xmlns:xs="http://www.w3.org/2001/XMLSchema<span style="color:Black; " <span style="color:Red; xmlns:msdata<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; urn:schemas-microsoft-com:xml-msdata<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; NewDataSet<span style="color:Black; " <span style="color:Red; msdata:IsDataSet<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; true<span style="color:Black; " <span style="color:Red; msdata:MainDataTable<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Test<span style="color:Black; " <span style="color:Red; msdata:UseCurrentLocale<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; true<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; complexType<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; choice <span style="color:Red; minOccurs<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 0<span style="color:Black; " <span style="color:Red; maxOccurs<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; unbounded<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Test<span style="color:Black; " <span style="color:Red; msdata:TotalRecordCount<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 5000<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; complexType<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; sequence<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Id<span style="color:Black; " <span style="color:Red; type<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; xs:int<span style="color:Black; " <span style="color:Red; minOccurs<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 0<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Name<span style="color:Black; " <span style="color:Red; type<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; xs:string<span style="color:Black; " <span style="color:Red; minOccurs<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 0<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Price<span style="color:Black; " <span style="color:Red; type<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; xs:double<span style="color:Black; " <span style="color:Red; minOccurs<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 0<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; sequence<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; complexType<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; choice<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; complexType<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; schema<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Test<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Id<span style="color:Blue; >1000<span style="color:Blue; </<span style="color:#A31515; Id<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Name<span style="color:Blue; >Laptop<span style="color:Blue; </<span style="color:#A31515; Name<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Price<span style="color:Blue; >40000<span style="color:Blue; </<span style="color:#A31515; Price<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; Test<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Test<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Id<span style="color:Blue; >1001<span style="color:Blue; </<span style="color:#A31515; Id<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Name<span style="color:Blue; >Monitor<span style="color:Blue; </<span style="color:#A31515; Name<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Price<span style="color:Blue; >5000<span style="color:Blue; </<span style="color:#A31515; Price<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; Test<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Test<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Id<span style="color:Blue; >1000<span style="color:Blue; </<span style="color:#A31515; Id<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Name<span style="color:Blue; >Mouse<span style="color:Blue; </<span style="color:#A31515; Name<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Price<span style="color:Blue; >200<span style="color:Blue; </<span style="color:#A31515; Price<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; Test<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; NewDataSet<span style="color:Blue; >
[/code]
View the full article