开发者

XML DataGrid Where Certain Nodes Are Empty

I am attempting to bind an XML file to a DataGrid. I am only bind the the "Transactions". What I cannot figure out is how to bind only data that has empty nodes. For example, the transaction that has "UserName" of "NSmith" does not have a value for "CustomerFirst". I want only this child to be bound to the DataGrid

<开发者_StackOverflow社区Root>

<Header>
  <value1>0000000</value1>
  <value2>1</value2>
  <value3>100.00</value3>
</Header>

<Transactions>
  <Txn>
    <id></id>
    <UserName>BSmith</User>
    <CustomerFirst>Bob</CustomerFirst>
    ...
  </Txn>
  <Txn>
    <id></id>
    <UserName>NSmith</User>
    <CustomerFirst></CustomerFirst>
    ...
  </Txn>
</Transactions>

</Root>

Here is my C# code:

serverPath = Server.MapPath("App_Data/" + xmlFileName);
DataSet dsBillPay = new DataSet();
dsBillPay.ReadXml(serverPath);
dgBillPay.DataSource = dsBillPay.Tables[1];
dgBillPay.DataBind();

The .Tables[1] is selecting the "Transactions". Now the question is selecting data that has empty nodes.

Thank you in advance.


You can use Linq-to-Xml to filter out elements that have all of their child-elements specified with values and include only those with missing data.

The following example retrieves users that have an empty element but allows AddressTwo to be empty.

string xmlText = @"<Root>
                    <Header>
                        <value1>0000000</value1>
                        <value2>1</value2>
                        <value3>100.00</value3>
                    </Header>

                    <Transactions>
                        <Txn>
                            <id>1</id>
                            <UserName>BSmith</UserName>
                            <CustomerFirst>Bob</CustomerFirst>
                        </Txn>
                        <Txn>
                            <id>2</id>
                            <UserName>NSmith</UserName>
                            <CustomerFirst></CustomerFirst>
                        </Txn>
                        <Txn>
                            <id></id>
                            <UserName>JSmith</UserName>
                            <CustomerFirst>James</CustomerFirst>
                        </Txn>
                        <Txn>
                            <id>4</id>
                            <UserName>KSmith</UserName>
                            <CustomerFirst>Kevin</CustomerFirst>
                            <AddressTwo></AddressTwo>
                        </Txn>
                    </Transactions>

                    </Root>";

var root = XElement.Parse(xmlText);

    var elementsThatCanBeEmpty = new HashSet<XName>
                                     {
                                         XName.Get("AddressTwo")
                                     };

    var transactionsWithoutCustomerFirst =
        from transactions in root.Elements(XName.Get("Transactions")).Elements()
        where transactions.Elements().Any
            (
                el =>
                String.IsNullOrEmpty(el.Value) &&
                !elementsThatCanBeEmpty.Contains(el.Name)
            )
        select transactions;

foreach(var t in transactionsWithoutCustomerFirst)
{
    Console.WriteLine(t.Element(XName.Get("UserName")).Value);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜