简体   繁体   中英

I want to selected data from an XML file where element is empty

This is the code that I have worked out, but I don't know how to put the selected data into a gridview in C#.

XDocument doc = XDocument.Load(savePath);
XElement ele = doc.Root.Element("boatInSlip");

var addSlipToGrid = from slip in doc.Elements("slipList").Elements("slip")
                            where slip.Element("boatInSlip").Value == ""
                            select slip;
        foreach (var slip in addSlipToGrid)
        {
            //No idea what needs to come into here
            //(EDIT) This code seemed to put 1 char in each cell
            nullSlipGrid.DataSource = slip.Value;
            nullSlipGrid.DataBind();
        }

This is the XML data.

<slipList>
  <slip>
    <dock>Dock 1</dock>
<slipId>2</slipId>
<slipWidth>4</slipWidth>
<slipLength>12</slipLength>
<boatInSlip></boatInSlip>
<slipHeight>12</slipHeight>
<slipDoor>true</slipDoor>
  </slip>
</slipList>

I want to select all the 'slips' where 'boatInSlip' = "" and display them into the gridview nullSlipGrid.

(EDIT 1) This result was displayed in a gridview in 1 column downwards: D ock

1 2 4 1 2 1 2 true

You can load the xml in DataSet which will be converted in DataTables and you can assign DataTable as DataSource to your girdview .

DataSet ds = new DataSet();
ds.ReadXml(savePath);

nullSlipGrid.DataSource = ds.Tables[0];
nullSlipGrid.DataBind();

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