Results 1 to 7 of 7

Thread: LINQ Advice

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    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);
                }
    
     
    
            }

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    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>&lt;/|&lt;|&gt;|/&gt;</Expression>
          <OpeningElement></OpeningElement>
          <ClosingElement></ClosingElement>
        </SubRule>
    
        <SubRule SourceType="XAML" OutputType="BBCODE">
          <Name>Get Property Values</Name>
          <Expression>=&quot;[()\[\]\w#,. =:/-{}]+&quot;</Expression>
          <OpeningElement></OpeningElement>
          <ClosingElement></ClosingElement>
        </SubRule>
    
        <SubRule SourceType="XAML" OutputType="BBCODE">
          <Name>Get Object Names</Name>
          <Expression>(?&lt;=&lt;/|&lt;)[\w:.]+</Expression>
          <OpeningElement></OpeningElement>
          <ClosingElement></ClosingElement>
        </SubRule>
    
        <SubRule SourceType="XAML" OutputType="BBCODE">
          <Name>Get Property Names</Name>
          <Expression>\b[\w:.]+(?=\=&quot;)</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!

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: LINQ Advice

    And yes the program does know what data to expect and where (Or at least it should!)

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    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

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    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
  •  



Click Here to Expand Forum to Full Width