如何解析作为字符串从文本框传递的Xml值

时间:2016-06-28 19:24:01

标签: c# asp.net parsing

我是C#

的新手

我有一个textarea,我的输入文件将是:

<?xml version="1.0" encoding="ISO-8859-1"?>
 <DespatchAdvice>
<DespatchAdviceHeader>
<DespatchAdviceNumber>40H2YZT11100510</DespatchAdviceNumber>
<CustomerPO>1424540</CustomerPO>
</DespatchAdviceHeader>
<IMAccountNumber>MD40111111</IMAccountNumber>
<ShipToSuffix>000</ShipToSuffix>
<DateHeader>
<Date Type="Document">20100510</Date>
<Date Type="Despatch">20100421</Date>
<Date Type="Delivery">20100426</Date>
<Date Type="Order">20100421</Date>
</DateHeader>
<Address Type="ShipFrom">
<Name>INGRAM MICRO</Name>
<AddressLine1>12510 MICRO DRIVE</AddressLine1>
<City>MIRA LOMA</City>
<State>CA</State>
<PostalCode>917520000</PostalCode>
</Address>
<Address Type="ShipTo">
<Name>NAVAL HOSPITAL</Name>
<Attention>ATTN Ms. Smith</Attention>
<AddressLine1>CODE 33 M82CG65</AddressLine1>
<AddressLine2>2222 N Willow ST</AddressLine2>
<City>Willowville</City>
<State>CA</State>
<PostalCode>902100000</PostalCode>
<CountryCode>US</CountryCode>
</Address>
<ConsignmentHeader>
<SequenceNumber>1</SequenceNumber>
<NumberOfPackages>5</NumberOfPackages>
<CarrierCode>RG</CarrierCode>
<CarrierName>RPS Ground/FedEx Ground</CarrierName>
<UniversalScacCode>RPSI</ UniversalScacCode>
</ConsignmentHeader>
<MeasurementHeader>
<UnitOfMeasure>LBS</UnitOfMeasure>
<Weight>165</Weight>
</MeasurementHeader>
<LineHeader>
<LineItem LineNumber="1">
<Product SKU="M35621" DespatchQuantity="1">
<Item Type="UPC">0808736847162</Item>
<ManufacturerSKU>Q8754A</ManufacturerSKU>
<CustomerPartNumber>K1111111</CustomerPartNumber>
<SKUDescription1>42IN X 200FT UNIVERSAL INSTANT-PAPR</SKUDescription1>
<SKUDescription2>DRY GLOSS PHOTO PAPER</SKUDescription2>
<SerialNumberHeader>
<SerialNumber/>
</SerialNumberHeader>
</Product>
<PackageHeader>
<IdentificationHeader>
<Identification TrackingNumber="1111111111111"/>
</IdentificationHeader>
</PackageHeader>
</LineItem>
<LineItem LineNumber="1">
<Product SKU="M35621" DespatchQuantity="1">
<Item Type="UPC">0808736847162</Item>
<ManufacturerSKU>Q8754A</ManufacturerSKU>
<CustomerPartNumber> K1111111</CustomerPartNumber>
<SKUDescription1>42IN X 200FT UNIVERSAL INSTANT-PAPR</SKUDescription1>
<SKUDescription2>DRY GLOSS PHOTO PAPER</SKUDescription2>
<SerialNumberHeader>
<SerialNumber/>
</SerialNumberHeader>
</Product>
</LineItem>
<LineItem LineNumber="1">
<Product SKU="M35621" DespatchQuantity="1">
<Item Type="UPC">0808736847162</Item>
<ManufacturerSKU>Q8754A</ManufacturerSKU>
<CustomerPartNumber> K1111111</CustomerPartNumber>
<SKUDescription1>42IN X 200FT UNIVERSAL INSTANT-PAPR</SKUDescription1>
<SKUDescription2>DRY GLOSS PHOTO PAPER</SKUDescription2>
<SerialNumberHeader>
<SerialNumber/>
</SerialNumberHeader>
</Product>
<PackageHeader>
<IdentificationHeader>
<Identification TrackingNumber="3333333333333"/>
</IdentificationHeader>
</PackageHeader>
</LineItem>
<LineItem LineNumber="1">
<Product SKU="M35621" DespatchQuantity="1">
<Item Type="UPC">0808736847162</Item>
<ManufacturerSKU>Q8754A</ManufacturerSKU>
<CustomerPartNumber> K1111111</CustomerPartNumber>
<SKUDescription1>42IN X 200FT UNIVERSAL INSTANT-PAPR</SKUDescription1>
<SKUDescription2>DRY GLOSS PHOTO PAPER</SKUDescription2>
<SerialNumberHeader>
<SerialNumber/>
</SerialNumberHeader>
</Product>
 </LineItem>
<LineItem LineNumber="1">
<Product SKU="M35621" DespatchQuantity="1">
<Item Type="UPC">0808736847162</Item>
<ManufacturerSKU>Q8754A</ManufacturerSKU>
<CustomerPartNumber> K1111111</CustomerPartNumber>
<SKUDescription1>42IN X 200FT UNIVERSAL INSTANT-PAPR</SKUDescription1>
<SKUDescription2>DRY GLOSS PHOTO PAPER</SKUDescription2>
<SerialNumberHeader>
<SerialNumber/>
</SerialNumberHeader>
</Product>
<PackageHeader>
<IdentificationHeader>
<Identification TrackingNumber="5555555555555"/>
</IdentificationHeader>
</PackageHeader>
</LineItem>
</LineHeader>
<TotalLinesShipped>5</TotalLinesShipped>
</DespatchAdvice>

现在我想在飞行中解析这个文件,意味着当我回发它时应该解析被解析为字符串的XML并根据需要存储该值。我不明白我怎么能实现它。我试过的代码是这样的,它确实按照需要在XML文件中下载文件,但它在一行中编码并编码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Threading;

public partial class Services_Ingram_Test : System.Web.UI.Page
{
    public Dictionary<string, List<string>> myCodes = new Dictionary<string, List<string>>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.ContentLength > 0)
        {
            try
            {
                byte[] buf = new byte[Request.ContentLength];
                buf = Request.BinaryRead(Request.ContentLength);
                string s = Encoding.UTF8.GetString(buf); 



                using (StreamWriter Streamwriter = new StreamWriter(HttpContext.Current.Server.MapPath("~/temp/") + System.DateTime.Now.ToFileTimeUtc().ToString() + ".xml"))
                {
                    Streamwriter.Write(s);
                    Streamwriter.Close();
                    Streamwriter.Dispose();
                }
                UpdateLogFile("Data saved");
                //if (!IsResponseASN(s))
                //    UpdatePOWithConfirmation(s);

                Response.ContentType = "Text/XML";
                Stream sw = Response.OutputStream;
                byte[] byteArray = Encoding.UTF8.GetBytes("OK");

                sw.Write(byteArray, 0, byteArray.Length);
                sw.Close();
            }
            catch (Exception ex)
            {
                Stream sw = Response.OutputStream;
                byte[] byteArray = Encoding.UTF8.GetBytes(ex.Message);
                sw.Write(byteArray, 0, byteArray.Length);
                sw.Close();
                //Response.Write(ex.Message.ToString());
                UpdateLogFile("Error: " + ex.ToString());
            }
        }
    }
    private void UpdateLogFile(string s)
    {

        using (StreamWriter Streamwriter = new StreamWriter(HttpContext.Current.Server.MapPath("~/temp/ack.log"), true))
        {
            Streamwriter.Write(DateTime.Now.ToString("MM-dd-yyyy hh:mm:ss"));
            Streamwriter.Write(s);
            Streamwriter.Write(Environment.NewLine);
            Streamwriter.Close();
            Streamwriter.Dispose();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

在您将xml作为字符串发布到网络服务器后尝试这样的事情:

var xDocument = System.Xml.Linq.XDocument.Parse("<somenode>Some values</somenode>");

答案 1 :(得分:0)

非常感谢你们的回答。我在输入String上使用了De-Serialization,而不是将String解析为XML。