filtering XML nodes containing a greater-than character in node text
I have a LINQ query from an XML file. I am filtering out certain nodes that have empty children, but also wanted to filter nodes which contain illegal characters.
From the example below, you can see that I am attempting to filter nodes that also have a greater-than character <
in them. (See child3 in XML sample)
However, this does not work. I can test for other characters inside the node like *
but <
does not work.
How can I modify my LINQ query to check for a greater-than character <
inside a node.
<Root>
<Header>
<total>123456</total>
</Header>
<Transactions>
<Txn>
<child1>BSmith</value1>
开发者_StackOverflow社区 <child2>Bob Smith</child2>
<child3>Acme Company<</child3>
<child4>Suite 3B</child4>
<child5>1234 Main Street</child5>
</Txn>
...
</Transactions>
</Root>
var root = XElement.Parse(xmlText);
var elementsThatCanBeEmpty = new HashSet<XName>
{
XName.Get("child4")
};
var transactionList = from transactions in
root.Elements(XName.Get("Transactions"))
.Elements().AsEnumerable()
where transactions.Elements().Any
(
el =>
(String.IsNullOrEmpty(el.Value) || el.Value.Contains("*")) &&
!elementsThatCanBeEmpty.Contains(el.Name)
)
select new
{
UserName = transactions.Element(XName.Get("child1")).Value
};
have a look at this. it is a very good starting point for you and you can use WHERE keyword to filter your XML data
http://www.switchonthecode.com/tutorials/introduction-to-linq-simple-xml-parsing
精彩评论