Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
<pre lang="text">
i have an asp.net application ...

*i have to fetch xml which includes xml header 


XML
<?xml version="1.0" encoding="utf-16"?><ProcessResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><ExtensionData /><ErrorCode>0123</ErrorCode><ErrorMessage /><Message>xyz.</Message></ProcessResponse>



i want yo extract the xml from above xml without header on asp page and needs my result in form of below :-


HTML
<ErrorCode>0123</ErrorCode><ErrorMessage /><Message>xyz.</Message></ProcessResponse>
Posted
Comments
George Jonsson 12-Sep-14 3:03am    
This is a repost of this question.
how to extract only xml from the string ?[^]
Why not continue this thread? You have a working answer there already.
Naveen Singh 12-Sep-14 3:13am    
It is not working for me and i have already tried..!!! but problem not resolved.
George Jonsson 12-Sep-14 3:27am    
Have you tried with:
Regex reg = new Regex(@"<errorcode>.*</message>", RegexOptions.IgnoreCase);
Naveen Singh 12-Sep-14 3:30am    
yes but no result .
George Jonsson 12-Sep-14 3:36am    
When I try that expression with your given data it works.
Update your first post with the code you are using.
You are either giving false information or have not implemented the given solution correctly.

1 solution

One method for reading and parsing XML data is to use System.Xml.XmlTextReader(). This will allow you to extract out exactly what you need from the XML.

This code example will demonstrate the basics of finding Elements and attributes. Since your situation doesn't require retrieving Attribute values, just remove the code for "if (reader.HasAttributes)".

The XmlTextReader class has many methods and properties that makes it very flexible.

Hope this helps.

C#
string XML = "<?xml version=\"1.0\" encoding=\"utf-16\"?><ProcessResponse xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><ExtensionData /><ErrorCode>0123</ErrorCode><ErrorMessage /><Message>xyz.</Message></ProcessResponse>";
System.Console.WriteLine(String.Format("\nOriginal XML String: {0}\n", XML));
if (XML != null && XML.Length > 0)
{
   // Convert XML String to XMLReader to call loadXMLSettings(XmlTextReader reader)
   StringReader sReader = new StringReader(XML);
   System.Xml.XmlTextReader xReader = new XmlTextReader(sReader);
   loadXMLSettings(xReader);
   xReader.Close();
}


C#
private static void loadXMLSettings(XmlTextReader reader)
{
    reader.WhitespaceHandling = WhitespaceHandling.None; // ignore whitespace
    while (reader.Read())
    {
        switch (reader.NodeType)
        {
            case XmlNodeType.Element: // The node is an element.
                Console.WriteLine("Element <{0}>", reader.Name);
                break;
            case XmlNodeType.Text: //Display the text in each element.
                Console.WriteLine("Value: {0}", reader.Value);
                break;
            case XmlNodeType.EndElement: //Display the end of the element.
                Console.WriteLine("</{0}>", reader.Name);
                break;
        }
        // Display all attributes.
        if (reader.HasAttributes)
        {
            Console.WriteLine("Attributes of <{0}>={1}", 
                reader.Name, reader.Value);
            for (int i = 0; i < reader.AttributeCount; i++)
            {
                reader.MoveToNextAttribute();
                Console.WriteLine(" {0}={1}", reader.Name, reader.Value);
            }
            // Attributes of a specific Element can be accessed
            //     directly by name using:
            String attribValue = reader.GetAttribute("version");
            Console.WriteLine("Attribute version has value of: {1}",
                reader.Name, attribValue);
        }
    }
}

Results:

Original XML String: <?xml version="1.0" encoding="utf-16"?><ProcessResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><ExtensionData /><ErrorCode>0123</ErrorCode><ErrorMessage /><Message>xyz.</Message></ProcessResponse>

Attributes of <xml>=version="1.0" encoding="utf-16"
version=1.0
encoding=utf-16
Attribute version has value of: 1.0
Element <ProcessResponse>
Attributes of <ProcessResponse>=
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xmlns:xsd=http://www.w3.org/2001/XMLSchema
Attribute version has value of:
Element <ExtensionData>
Element <ErrorCode>
Value: 0123
</ErrorCode>
Element <ErrorMessage>
Element <Message>
Value: xyz.
</Message>
</ProcessResponse>
 
Share this answer
 
v4

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