1

Can someone help me out in the best way to display a raw xml string to a browser in an xml formatted way?

The code i created below does not display anything to the it and gives in error:

  string xml = GetMessageXml(Request.QueryString["ID"].ToString());
            XDocument doc;
            using (StringReader s = new StringReader(xml.Substring(1)))
            {
                doc = XDocument.Load(s);
            }
            Response.ContentType = "text/xml";
            doc.Save(Response.Output);
            Response.Write(doc.ToString());

Error:
XML Parsing Error: junk after document element
'Location:H t t p: localhost/Accounts/EventLogMessageDetails.aspx?id=178'
Line Number 83, Column 9:'

this is the xml string:

?<?xml version="1.0" encoding="utf-8"?>
<Order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<OrderID>00000000-0000-0000-0000-000000000000</OrderID>
<IsOrderThrough>true</IsOrderThrough>
<VendorName>le</VendorName>
<OrderUniqueIdentifier>K03936</OrderUniqueIdentifier>
<SoldToCustomerID>A786</SoldToCustomerID>
<ShipToCustomerID>A786</ShipToCustomerID>
<OrderType>Standard</OrderType>
<CustomerPurchaseOrderNumber>PO0000336</CustomerPurchaseOrderNumber>
<ProjectName />
<EmailAddress>[email protected]</EmailAddress>
<DeliveryDate>2012-05-29T10:09:55.492696-05:00</DeliveryDate>
<ShipToAddress>
<AddressID>00075</AddressID>
<OrganizationName>SEBA-E</OrganizationName>
<AddressLine1>3700 STATE</AddressLine1>
<AddressLine2>Elk</AddressLine2>
<City>LA CROSSE</City>
<State>WI</State>
<ZipCode>54601</ZipCode>
<Country>US</Country>
<DaytimePhoneNumber>6782260680EXT</DaytimePhoneNumber>
</ShipToAddress>
<ShippingMethodName>FEDEX PRIORITY OVERNIGHT</ShippingMethodName>
<ShippingMethodID>F01</ShippingMethodID>
<MarketSegment>Commercial</MarketSegment>
<Comments>Elk^</Comments>
<LineItems>
<OrderLineItem>
<LineItemID>00000000-0000-0000-0000-000000000000</LineItemID>
<ProductID>Kbv</ProductID>
<Quantity>2</Quantity>
<ListPrice>10.67</ListPrice>
<PlacedPrice>3.84</PlacedPrice>
<DeliveryDate>2012-05-29T10:09:56.6957979-05:00</DeliveryDate>
<ShippingAddress>
<AddressID>Z00138075</AddressID>
<OrganizationName>moomoo</OrganizationName>
<AddressLine1>3700 STATE ROAD 16</AddressLine1>
<AddressLine2>moomoo</AddressLine2>
<City>LA CROSSE</City>
<State>WI</State>
<ZipCode>54601</ZipCode>
<Country>US</Country>
<DaytimePhoneNumber>675555550680EXT</DaytimePhoneNumber>
</ShippingAddress>
<ShippingMethodID>F01</ShippingMethodID>
<EmailAddress>[email protected]</EmailAddress>
<Comments />
<SequenceNumber>0</SequenceNumber>
</OrderLineItem>
</LineItems>
<BusinessUnit />
<FOBPoint>FB2</FOBPoint>
<Notify>TD</Notify>
<WorkOrder />
<SubmittedByUserName>TDAVIS</SubmittedByUserName>
<SpecialInstructions />
</Order>
7
  • The problem is in the XML you're getting. It's not able to parse it. Please show us the whole XML you're trying to write. My guess from the error message is that you have two root elements, i.e. the first Order is the first element, then you have another Order after it. This isn't a valid XML document. Besides that, you seem to be on the right track by using XDocument's ToString. Commented May 29, 2012 at 20:21
  • I think you should refer this SO post - stackoverflow.com/questions/350314/… Commented May 29, 2012 at 20:22
  • Why not use linq to XML ,what do you want to extract from xml ? Commented May 29, 2012 at 20:25
  • Can you help me with a linq solution? Commented May 29, 2012 at 20:31
  • Please example what do you want to do with this XML? Commented May 29, 2012 at 20:41

2 Answers 2

3

This is what I have did quickly.

Added Order.xml into my ASP.NET Project.

Create an Showorder.aspx page as and included C# code between <% and %> as below:

<body>
 <form id="form1" runat="server">
  <div>
   <% 
     string xml = Request.QueryString["ID"].ToString();
        XDocument doc;
        doc = XDocument.Load(xml);
        Response.Write("<XMP>"+ doc.ToString()+"<\\XMP>");
        %>
    </div>
  </form>
</body>

After that I launched my page as below:

  http://localhost:52134/showxml.aspx?ID=http://localhost:52134/order.xml

I do get the XML as below: (Note: Be sure to use XMP ..../XMP otherwise you will not see formatted XML in browser)

enter image description here

Feel free to experiment the code and try any way you would want.

Sign up to request clarification or add additional context in comments.

1 Comment

How do I cut the junk out of my xml to make it in good form to use this code?
0

Your problem is with your input XML as it is BAD XML.

Look at your error:

Line Number 83, Column 9:</Order><Order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

Your XML has XML Namespace defined at line #83 with "Order" element which should be defined at the top. It seems when you merged the XML or created, you just botched it.

Also what do you mean "display a raw xml string to a browser in an xml formatted way"? Do you mean " display a raw string to a browser in an xml formatted way"? Please explain what you input is and how do u want to display in browser with example..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.