1

有人可以以最佳方式帮助我以 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 解析错误:文档元素
'Location:H ttp: 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>
4

2 回答 2

3

这是我快速完成的。

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

创建一个 Showorder.aspx 页面,并在 <% 和 %> 之间包含 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)

在此处输入图像描述

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

于 2012-05-29T21:40:25.773 回答
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..

于 2012-05-29T20:35:20.377 回答