Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please guys, i'm trying to read the following XML String, but it seems like there's something i'm doing wrong here. Here is the XML String

XML
<?xml version="1.0" encoding="utf-8" ?>
<TRANS_NET>
<REQUEST_ID>0</REQUEST_ID>
<REQUEST_DATE>0000-000</REQUEST_DATE>
<RESPONSE_CODE>003</RESPONSE_CODE>
<RESPONSE_REASON>
<ERROR>
<ERROR_CODE>243</ERROR_CODE>
<ERROR_CODE>003</ERROR_CODE>
<ERROR_CODE>040</ERROR_CODE>
<ERROR_CODE>017</ERROR_CODE>
<ERROR_CODE>039</ERROR_CODE>
</ERROR>
</RESPONSE_REASON>
</TRANS_NET>


Here Is my Code for reading it
C#
using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(strReader))
                {
                    while (xmlReader.Read())
                    {
                        if (xmlReader.NodeType == System.Xml.XmlNodeType.Element)
                        {
                            //if (xmlReader.Name == "TRANS_NET")
                            //{
                                switch (xmlReader.Name)
                                {
                                    case "TRANS_NET":
                                        plainText += "Response \r\n";
                                        break;
                                    case "REQUEST_ID":
                                        plainText += String.Format("Request ID = {0} \r\n", xmlReader.Value);
                                        break;
                                    case "REQUEST_DATE":
                                        plainText += String.Format("Request Date = {0} \r\n", xmlReader.Value);
                                        break;
                                    case "RESPONSE_CODE":
                                        plainText += String.Format("Response Code = {0} \r\n", xmlReader.Value);
                                        break;
                                    case "RESPONSE_REASON":
                                        plainText += "An Error Was Encountered \r\n";
                                        break;
                                    case "ERROR":
                                        plainText += "Error Codes List \r\n";
                                        break;
                                    case "ERROR_CODE":
                                        if (xmlReader.Read())
                                        {
                                            plainText += String.Format("Error Code = {0} \r\n", xmlReader.Value);
                                        }
                                        break;
                                    default:
                                        plainText += "\r\n";
                                        break;
                                }
                            //}
                        }
                    }
                }


And here is the result that i get
Response
Request ID =
Request Date =
Response Code =
An Error Was Encountered
Error Codes List
Error Code = 243
Error Code = 003
Error Code = 040
Error Code = 017
Error Code = 039

The problem here is that the
Request ID =
Request Date =
Response Code = Values are not display

Thanks
Posted

1 solution

In xml, when talking about object model (xml document model), the values themselves are nodes of type text. You can see it while debugging your code. The first read brings the node as element and the next brings the value as a text.
You can see in your own code that ERROR_CODE treated differently, and it's work well...
 
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