以XML格式向浏览器显示XML字符串

时间:2012-05-29 20:18:33

标签: c# asp.net

有人能以最佳方式帮助我以xml格式化方式向浏览器显示原始xml字符串吗?

我在下面创建的代码没有显示任何内容并错误地给出:

  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());

错误:
XML解析错误:文档元素之后的垃圾 '位置:H t t p:localhost / Accounts / EventLogMessageDetails.aspx?id = 178'
第83行,第9栏:'

这是xml字符串:

?<?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>Nair@ecomkva.com</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>IS@Cnj.com</EmailAddress>
<Comments />
<SequenceNumber>0</SequenceNumber>
</OrderLineItem>
</LineItems>
<BusinessUnit />
<FOBPoint>FB2</FOBPoint>
<Notify>TD</Notify>
<WorkOrder />
<SubmittedByUserName>TDAVIS</SubmittedByUserName>
<SpecialInstructions />
</Order>

2 个答案:

答案 0 :(得分:3)

这就是我所做的很快。

将Order.xml添加到我的ASP.NET项目中。

创建一个Showorder.aspx页面,并在&lt;%和%&gt;之间包含C#代码。如下:

<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>

之后我按照以下方式启动了我的页面:

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

我确实得到如下XML :(注意:一定要使用XMP .... / XMP,否则你不会在浏览器中看到格式化的XML)

enter image description here

随意尝试代码并尝试任何您想要的方式。

答案 1 :(得分:0)

您的输入XML存在问题,因为它是BAD XML。

查看错误

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

您的XML具有在第83行定义的XML命名空间,其中“Order”元素应在顶部定义。看来,当您合并XML或创建XML时,您只是将其搞砸了。

另外你的意思是“以xml格式的方式向浏览器显示原始xml字符串”?您的意思是“以xml格式显示原始字符串到浏览器”?请解释您输入的内容以及您希望如何在浏览器中显示示例..