Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML
Hi,
Going for something like reading an xml file which looks like this:
<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]
 
 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 13-Feb-13 4:41am
09hadi703
Edited 13-Feb-13 8:20am
v7
Comments
Chris Reynolds (BNY) at 13-Feb-13 10:45am
   
What have you tried?
richcb at 13-Feb-13 10:45am
   
Where is your XML data coming from, document, web service, etc?
Jegan Thiyagesan at 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 at 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
ryanb31 at 13-Feb-13 10:47am
   
Look into the XMLDocument object. What have you done so far?
09hadi at 13-Feb-13 10:57am
   
reader.ReadToFollowing("alarm");
reader.MoveToFirstAttribute();
reader.MoveToNextAttribute();
string Alarm_ID = reader.Value;
MessageBox.Show(" Alarm ID is: " + Alarm_ID);
//////////////////////////////////////////////////////////////////
reader.ReadToFollowing("attribute");
string name = reader.ReadElementContentAsString().ToString();
MessageBox.Show("Name is: " + name);
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
reader.ReadToFollowing("0x11f56");
string severity = reader.ReadElementContentAsString().ToString();
MessageBox.Show("Severity is: " + severity);
this is what I have so far but this is giving me an error at the line with string name
CHill60 at 13-Feb-13 11:07am
   
What is the error that is reported?
09hadi at 13-Feb-13 11:14am
   
The method is not supported on node type None
CHill60 at 13-Feb-13 11:29am
   
I think the problem is with the line reader.ReadToFollowing("attribute"); - and reader therefore has no "current element".
As an aside ReadElementContentAsString returns a string so you don't need the .ToString() after it - I've done that and been flamed in this forum so just trying to save you some pain :-)
09hadi at 13-Feb-13 10:58am
   
and the xml is coming from a web service
PIEBALDconsult at 13-Feb-13 11:06am
   
I use an XmlDocument and then use XPath. BUT! The namespace may cause you trouble. Default namespaces are the worst.
09hadi at 13-Feb-13 11:12am
   
can u elaborate please?
PIEBALDconsult at 13-Feb-13 11:15am
   
Have you used an XmlDocument?
09hadi at 13-Feb-13 11:24am
   
no
joshrduncan2012 at 13-Feb-13 11:24am
   
I agree, LinQ to XML may be an option.
09hadi at 13-Feb-13 11:27am
   
I have got the previous technique to work, but the problem is that it is skipping an attribute
Sergey Alexandrovich Kryukov at 15-May-13 11:42am
   
Let me tell you that all your "answers" look like cheating. You are answering your own questions and even self-accept then. It looks like you pretend that you are helping someone else.
Please stop it. A number of members already lost their accounts as a result of abuse reports.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
09hadi at 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 at 13-Feb-13 11:39am
   
or is there anyway I could get all the values in a node using xmlreader?
Jegan Thiyagesan at 13-Feb-13 12:19pm
   
Or use xml serialiser, create an object that contains the xml attributes and parse it.

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 853
1 OriginalGriff 410
2 CPallini 275
3 George Jonsson 226
4 Richard Deeming 145
0 OriginalGriff 5,450
1 CPallini 4,500
2 Sergey Alexandrovich Kryukov 4,482
3 George Jonsson 3,057
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100