Click here to Skip to main content
15,945,603 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below is my xml i need to read this xml and display in gridview with below columns 

Bnum    Pnum   Vnum    Bdilute   Cdilute   Test   Mcode and the data for that columns should be brought from below xml 

For Bnum we need to get SID from view1 tag of xml
For Pnum we need to get IDPa from view2 tag of xml
For Vnum we need to get ID from Act1 tag of xml
For Bdilute we need to get TrDate from view1 tag of xml
For Cdilute we need to get Code from Act1,Act2 and Act3 tag of xml
For Test we need to get Type from Dial1 tag of xml
For Mcode we need to get Co from Dial1 tag of xml


-<view>

<view1>

<SID>MOH-F-1000076</SID>  --required

<RID>TPA001</RID>

<TrDate>13/03/2018 08:42</TrDate>  --required

</view1>


-<view2>

<ID>IP0000012694</ID>

<IDPa>5653584</IDPa>  --required

<MeID>02293010185</MeID>


-<count>

<FID>MOH-F-1000076</FID>

<Start>01/08/2017 00:09</Start>

<End>02/08/2017 19:48</End>

</count>


-<Dial1>

<Type>Principal</Type>  --required

<Co>K35.80</Co> --required

</Dial1>


-<Dail2>

<Type>Secondary</Type>  --required

<Co>R10.9</Co>  --required
 
</Dail2>


-<Dial3>

<Type>Secondary</Type>  --required

<Code>R11.0</Code>  --required

</Dial3>


-<Dial4>

<Type>Secondary</Type>  --required
 
<Code>R63.8</Code>  --required

</Dial4>


-<Dial5>

<Type>Secondary</Type>   --required

<Code>R79.89</Code>  --required

</Dial5>


-<Dial6>

<Type>Secondary</Type>  --required

<Code>Z01.812</Code>  --required

</Dial6>


-<Dial7>

<Type>Secondary</Type>  --required

<Code>Z01.83</Code>  --required

</Dial7>


-<Act1>

<ID>1</ID>    --required

<Code>36000</Code>  --required


</Act1>


-<Act2>

<ID>2</ID>  --required

<Code>85396</Code>  --required

</Act2>


-<Act3>

<ID>3</ID>  --required

<Code>86900</Code>  --required

</Act3>


</view2>
</view>


What I have tried:

can anyone help us out as we are unable to retrieve them
Posted
Updated 13-Mar-18 23:48pm
Comments
F-ES Sitecore 14-Mar-18 5:35am    
You're going to have to convert your XML into a list of objects that hold the data you want. To find out how to do this google "c# deserialise xml". If the xml is dynamic, in that you don't know how many "act" or "dial" nodes there are then basic deserialisation might not work, instead you might have to just parse the data yourself to extract what you need (google "c# custom xml deserialisation"). Once the data is in a List or of objects you can then bind that to your gridview.

Please don't ask me to write the code for you, this is not a code-writing service, look into the various things I've mentioned above and at least attempt the code yourself.
Laxmidhar tatwa technologies 14-Mar-18 6:48am    
If u stored it in a file
Use namespace system.XML
Xmldocument doc = new xmldocument();
doc.load(XML file path);
In for each loop get each node
Then get the value of each node and subnode
kav@94 14-Mar-18 6:53am    
That is what i am unable to get it i was already trying in the same way
Laxmidhar tatwa technologies 14-Mar-18 7:49am    
csharp.net-infomations.com/XML/
how-to-read-xml.htm
kav@94 14-Mar-18 7:58am    
below is the code which i have tried as suggested by you
try
{
XmlDocument Docs = new XmlDocument();
Docs.Load(Server.MapPath("~/Sample.xml"));
XmlElement root = Docs.DocumentElement;
DataTable dt = new DataTable();
dt.Columns.Add("Bnum");
dt.Columns.Add("Pnum");
dt.Columns.Add("Vnum");
dt.Columns.Add("Vnum");
dt.Columns.Add("Cdilute");
dt.Columns.Add("Test");
dt.Columns.Add("Mcode");
XmlNodeList nodes = root.SelectNodes("????????");
foreach (XmlNode node in nodes)
{
dt.Rows.Add(node["SID"].InnerText.ToString(),
node["Pnum"].InnerText.ToString(),
node["Vnum"].InnerText.ToString(),
node["Vnum"].InnerText.ToString(),
node["Code"].InnerText.ToString(),
node["Cdilute"].InnerText.ToString(),
node["Mcode"].InnerText.ToString()
);
}
return dt;
}
catch (Exception es)
{
throw es;
}
but it is going into catch block and giving me the exception object reference not set to instance of object.I am unable to get how to provide the relation in this line XmlNodeList nodes = root.SelectNodes("????????"); to get the value of get the value of each node and subnode

1 solution

Have you looked at the following article XML Reader?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900