Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
Going for something like reading an xml file which looks like this:
XML
<alarm-response-list xmlns="http://www.ca.com/spectrum/restful/schema/response" error="EndOfResults" throttle="284" total-alarms="284">
<alarm-responses>
<alarm id="50e1c57a-6860-100e-01a7-0019bb29739c">
<attribute id="0x1006e">202.125.155.112</attribute>
<attribute id="0x11f56">2</attribute>
<attribute id="0x12b4c">WIDE-AREA PROBABLE LINK FAILURE</attribute>
<attribute id="0x11f4e">1356973434</attribute>
<attribute id="0x1296e">
The contact status of WA Link model ( name - 202.125.155.112, type - WA_Link ) is in a "disabled" state.
</attribute>
<attribute id="0x12d7f">202.125.155.112</attribute>
</alarm>
<alarm id="510e5569-8843-1030-01a7-0019bb29739c">
<attribute id="0x1006e">ZTBL-21271-DSL-Primary-Central</attribute>
<attribute id="0x11f56">1</attribute>
<attribute id="0x12b4c">DUPLICATE MODEL DETECTED</attribute>
<attribute id="0x11f4e">1359893865</attribute>
<attribute id="0x1296e">
Device ZTBL-21271-DSL-Primary-Central of type Pingable has an IP address (192.168.115.254) and MAC address (0) that are used by one or more other models. The other models are: Model: NBP-NA-DSL-Primary-South Type: Pingable In: Universe:NBP South Zone
</attribute>
<attribute id="0x12d7f">192.168.115.254</attribute>
</alarm>

I need to get the values between the tags using c#. Please help me with the code.

[Edit] - Adding further information from OPs comments [CHill60]

C#
reader.ReadToFollowing("alarm");
                   reader.MoveToFirstAttribute();

                       //////////////////////////////////////////////////////////////////
                       reader.ReadToFollowing("attribute");
                       string name = reader.ReadElementContentAsString().ToString();
                       MessageBox.Show("Name is: " + name);
                       //////////////////////////////////////////////////////////////////
                       //////////////////////////////////////////////////////////////////
                       reader.ReadToFollowing("attribute");
                       string cause = reader.ReadElementContentAsString().ToString();
                       MessageBox.Show("Severity is: " + cause);
                       //////////////////////////////////////////////////////////////////
                       //////////////////////////////////////////////////////////////////
                       reader.ReadToNextSibling("attribute");
                       string variab = reader.ReadElementContentAsString().ToString();
                       MessageBox.Show("Cause is: " + variab);
                       //////////////////////////////////////////////////////////////////
                       //////////////////////////////////////////////////////////////////
                       reader.ReadToFollowing("attribute");
                       string name1 = reader.ReadElementContentAsString().ToString();
                       MessageBox.Show("Name is: " + name1);
                       //////////////////////////////////////////////////////////////////
                       //////////////////////////////////////////////////////////////////
                       reader.ReadToFollowing("attribute");
                       string cause1 = reader.ReadElementContentAsString().ToString();
                       MessageBox.Show("Severity is: " + cause1);
                       //////////////////////////////////////////////////////////////////
                       //////////////////////////////////////////////////////////////////
                       reader.ReadToNextSibling("attribute");
                       string variab1 = reader.ReadElementContentAsString().ToString();
                       MessageBox.Show("Cause is: " + variab1);
                       //////////////////////////////////////////////////////////////////


this is what I have so far but this piece of code is skipping one attribute type. The output is:
Quote:
202.125.155.112

Quote:
WIDE-AREA PROBABLE LINK FAILURE

Quote:
202.125.155.112

and then it moves on to the next node



And the XML is coming from a web service.
Posted
Updated 13-Feb-13 8:20am
v7
Comments
Chris Reynolds (UK) 13-Feb-13 10:45am    
What have you tried?
Richard C Bishop 13-Feb-13 10:45am    
Where is your XML data coming from, document, web service, etc?
Jegan Thiyagesan 13-Feb-13 10:46am    
You expect someone to write the code for you? there are plenty of example on google how to read xml file.
09hadi 13-Feb-13 10:59am    
I dont expect someone to write the code for me. as u can see I have posted my efforts below
ZurdoDev 13-Feb-13 10:47am    
Look into the XMLDocument object. What have you done so far?

Hello,

You have multiple approaches for reading an XML DOM:
1) Use an XML Reader, very fast and doesn't consume too much memory, but not too developer friendly
2) Use XPath queries in the SelectNodes / SelectSingleNode methods of XMLNode, that solution can be memory-heavy but is more developer friendly
3) Use an XML Navigator (actually it is an XPathNavigator)

Depending on your requirements, I'd recommend using either the first or the second options.
Requirements would be based upon the memory and efficiency constraints.

Hope this help
 
Share this answer
 
Comments
09hadi 13-Feb-13 11:39am    
can u modify or tell me something related to the technique I have mentioned. It is retrieving attributes but missing a line
09hadi 13-Feb-13 11:39am    
or is there anyway I could get all the values in a node using xmlreader?
Jegan Thiyagesan 13-Feb-13 12:19pm    
Or use xml serialiser, create an object that contains the xml attributes and parse it.
reader.ReadToFollowing("alarm");
                    reader.MoveToFirstAttribute();
                    for (int j = 0; j <= i; j++)
                    {
                        //////////////////////////////////////////////////////////////////
                        reader.ReadToFollowing("attribute");
                        string name = reader.ReadElementContentAsString().ToString();
                        //MessageBox.Show("Name is: " + name);
                        //////////////////////////////////////////////////////////////////
                        //////////////////////////////////////////////////////////////////
                        reader.MoveToAttribute("attribute");
                        string severity = reader.ReadElementContentAsString().ToString();
                        //MessageBox.Show("Severity is: " + severity);
                        //////////////////////////////////////////////////////////////////
                        //////////////////////////////////////////////////////////////////
                        reader.MoveToAttribute("attribute");
                        string cause = reader.ReadElementContentAsString().ToString();
                        //MessageBox.Show("Cause is: " + cause);
                        //////////////////////////////////////////////////////////////////
                        //////////////////////////////////////////////////////////////////
                        reader.MoveToAttribute("attribute");
                        string Time = reader.ReadElementContentAsString().ToString();
                        double timestamp = Convert.ToDouble(Time);

                        System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);

                        dateTime = dateTime.AddSeconds(timestamp);
                        dateTime = dateTime.AddHours(5);

                        string printDate = dateTime.ToShortDateString() + " , " + dateTime.ToShortTimeString();

                        string[] arr = printDate.Split(',');

                        string date = arr[0];
                        string time = arr[1];


                        //MessageBox.Show("Time is:" + time + " Date is :" + date);
                        //////////////////////////////////////////////////////////////////
                        //////////////////////////////////////////////////////////////////
                        reader.MoveToAttribute("attribute");
                        string nativ = reader.ReadElementContentAsString().ToString();
                        //MessageBox.Show("NativeCause is: " + nativ);
                        //////////////////////////////////////////////////////////////////
                        //////////////////////////////////////////////////////////////////
                        reader.MoveToAttribute("attribute");
                        string ip = reader.ReadElementContentAsString().ToString();
                        //MessageBox.Show("IP is: " + ip);
                        //////////////////////////////////////////////////////////////////
                        inserthistory(name, "0", nativ, date, time, ip);
 
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