|
-
Mar 31st, 2009, 11:18 AM
#1
Thread Starter
Frenzied Member
LINQ Advice
So I was speaking to Mendhak earlier about this and whether xPath would be better. So heres the code:
Code:
private void PopulateList(String SourceType, String OutputType)
{
//Populate The ListOfSubRules Property using LINQ to XML
XDocument XMLDataStore = XDocument.Load("DataStore.xml");
var XMLSubRules = from XMLSubRule in XMLDataStore.Descendants("SubRule")
where (XMLSubRule.Attribute("SourceType").Value == SourceType && XMLSubRule.Attribute("OuputType").Value == OutputType)
select new
{
Expression = XMLSubRule.Element("Expression").Value,
OpeningElement = XMLSubRule.Element("OpeningElement").Value,
ClosingElement = XMLSubRule.Element("ClosingElement").Value
};
foreach (var XMLSubRule in XMLSubRules)
{
SubRule SubRuleItem = new SubRule(XMLSubRule.Expression,
XMLSubRule.OpeningElement,
XMLSubRule.ClosingElement);
ListOfSubRules.Add(SubRuleItem);
}
}
-
Mar 31st, 2009, 11:21 AM
#2
Thread Starter
Frenzied Member
Re: LINQ Advice
This is the Data it is operating on:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<Store>
<SubRules>
<SubRule SourceType="XAML" OutputType="BBCODE">
<Name>Get Tags</Name>
<Expression></|<|>|/></Expression>
<OpeningElement></OpeningElement>
<ClosingElement></ClosingElement>
</SubRule>
<SubRule SourceType="XAML" OutputType="BBCODE">
<Name>Get Property Values</Name>
<Expression>="[()\[\]\w#,. =:/-{}]+"</Expression>
<OpeningElement></OpeningElement>
<ClosingElement></ClosingElement>
</SubRule>
<SubRule SourceType="XAML" OutputType="BBCODE">
<Name>Get Object Names</Name>
<Expression>(?<=</|<)[\w:.]+</Expression>
<OpeningElement></OpeningElement>
<ClosingElement></ClosingElement>
</SubRule>
<SubRule SourceType="XAML" OutputType="BBCODE">
<Name>Get Property Names</Name>
<Expression>\b[\w:.]+(?=\=")</Expression>
<OpeningElement></OpeningElement>
<ClosingElement></ClosingElement>
</SubRule>
</SubRules>
<FormatBoxes>
</FormatBoxes>
</Store>
And to be honest I'm not happy with the document structure either I would like to change that!
-
Mar 31st, 2009, 11:22 AM
#3
Thread Starter
Frenzied Member
Re: LINQ Advice
And yes the program does know what data to expect and where (Or at least it should!)
-
Mar 31st, 2009, 11:38 AM
#4
Re: LINQ Advice
So with the XPathNavigator you'd use something like
Code:
foreach(XpathNavigator subRuleNode in xmlDataStore.Select("/Store/SubRules/SubRule"))
{
SubRule subRuleItem = new SubRule(subRuleNode.SelectSingleNode("Expression").Value, subRuleNode.SelectSingleNode("OpeningElement").Value, subRuleNode.SelectSingleNode("ClosingElement").Value
ListOfSubRules.Add(subRuleItem);
}
My gripe with .Descendants is that it is equivalent to
Code:
xmlDataStore.Select("//SubRule")
which is equivalent to
Code:
myXmlDocument.SelectNodes("//SubRule")
which does a recursive search across node names. The // (double slash) was introduced for 'unknown' XML data, in which you knew a node existed, but didn't know the XML structure or where the node would be.
Since you mentioned that this is going to be read-only, I would use XpathNavigator over XmlDocument.
-
Mar 31st, 2009, 11:46 AM
#5
Thread Starter
Frenzied Member
Re: LINQ Advice
Cool, My only issue with that code is that it gets EVERY subrule item, which I do not what. Can I add a LINQ satement here to query the data more specificly?
Code:
SubRule subRuleItem = new SubRule(subRuleNode.SelectSingleNode("Expression").Value, subRuleNode.SelectSingleNode("OpeningElement").Value, subRuleNode.SelectSingleNode("ClosingElement").Value
-
Mar 31st, 2009, 02:36 PM
#6
Re: LINQ Advice
Oh! I see, you're calling it in a function. Yeah, that's easily done too.
Code:
foreach(XpathNavigator subRuleNode in xmlDataStore.Select("/Store/SubRules/SubRule[@SourceType='" + theParameterPassedToThisFunction + ']"))
{
SubRule subRuleItem = new SubRule(subRuleNode.SelectSingleNode("Expression").Value, subRuleNode.SelectSingleNode("OpeningElement").Value, subRuleNode.SelectSingleNode("ClosingElement").Value
ListOfSubRules.Add(subRuleItem);
}
The [] square brackets is like a 'where', and the @SourceType means the @ttribute named SourceType.
-
Mar 31st, 2009, 03:13 PM
#7
Thread Starter
Frenzied Member
Re: LINQ Advice
Ok, I see what you doing. I must look up more XML and LINQ as im sure there is a better method than .Descendants. I will have a look at xpath too and the DataStore's structure and see how I go from there.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|