简体   繁体   中英

xDocument.Load returning null?

From debug I can check that Stream is passing

<POS>
  <Id>4</Id>
  <FolderPath>C://FolderPath</FolderPath>
  <Number>44</Number>
  <POSTillTypeRecord>Tilltype</POSTillTypeRecord>
  <StockLocationRecord>StockLocationRec</StockLocationRecord>
</POS>

to:

public XmlActionResult Upload()
    {
        //List<POSUnitRecord> POSs = null;

        try
        {
            using (var up = new StreamReader(Request.InputStream))
            {   

                POSUnitRecord POS =
                        (
                               from e in XDocument.Load(up).Root.Elements("Payment")  //POS")
                               //select (string)e.Element("POS")                                  
                               select new POSUnitRecord
                               {
                                   Id = Int32.Parse((string)e.Element("Id")),
                                   FolderPath = (string)e.Element("FolderPath"),
                                   Number = Int32.Parse((string)e.Element("Number")),

                                   //POSTillTypeRecord = (string)e.Element("POSTillTypeRecord"),
                                   //StockLocationRecord = (string)e.Element("StockLocationRecord")                                   
                               }
                           ).FirstOrDefault();

why is POSUnitRecord POS returning NULL. Something to do with payment/POS?

Your XML do not have Payment elements under root. So you have empty sequence. Getting FirstOrDefault() from empty sequence gives you null .

I suggest you to load xml into XElement directly:

var p = XElement.Load(reader);
var pos = new POSUnitRecord
{
    Id = (int)p.Element("Id"),
    FolderPath = (string)p.Element("FolderPath"),
    Number = (int)p.Element("Number")
};

Side note - you can use Xml Serialization here:

[XmlRoot("POS")]
public class POSUnitRecord
{
    public int Id { get; set; }
    public string FolderPath { get; set; }
    public int Number { get; set; }
}

And deserialization is simple:

using (var reader = new StreamReader(Request.InputStream))
{
    XmlSerializer serializer = new XmlSerializer(typeof(POSUnitRecord));
    var pos = (POSUnitRecord)serializer.Deserialize(reader);
}

You're selecting "Payment" element which doesn't exist, but there is a "POS" element that does. Further, that element becomes Root . Finally, no need to use Linq syntax; you can use methods provided by XLinq:

var xDoc = XDocument.Load(up);
var posUnitRecords = xDoc.Root.Select(e => new POSUnitRecord()
{
    Id = Int32.Parse((string)e.Element("Id").Value),
    FolderPath = (string)e.Element("FolderPath").Value,
    Number = Int32.Parse((string)e.Element("Number").Value),

});
var firstRecord = POSUnitRecords.First();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM