5 Replies - 609 Views - Last Post: 20 February 2013 - 07:12 AM Rate Topic: -----

#1 DaedalusAero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 04-July 12

sgmlReader - why is my query finding no results

Posted 20 February 2013 - 05:50 AM

I'm trying to query an SGML file with sgmlReader, but the query is not finding any results. The code I have is:

string[] projectFiles = Directory.GetFiles(path, typeExtention, SearchOption.AllDirectories);

                foreach (string file in projectFiles)
                {


                    // setup SgmlReader
                    var sgmlReader = new Sgml.SgmlReader();
                    var myStringreader = new StringReader(File.ReadAllText(file));



                    sgmlReader.DocType = "HTML";
                    sgmlReader.WhitespaceHandling = WhitespaceHandling.All;
                    sgmlReader.CaseFolding = Sgml.CaseFolding.ToLower;
                    sgmlReader.InputStream = myStringreader;


                    XElement root = XElement.Load(sgmlReader);
                    IEnumerable<XElement> selectedElement =
                        from el in root.Elements("security")
                        where (string)el.Attribute("class") == "3"
                        select el; 

                    foreach (XElement el in selectedElement)
                        MessageBox.Show("text" + el);

                }


In the document i'm querying i clearly have the element and attribute <security class="3"> so i can't see why it's not finding it. I've debugged and it's looking in the right place for the file etc. Help appreciated. Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: sgmlReader - why is my query finding no results

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3566
  • View blog
  • Posts: 11,082
  • Joined: 05-May 12

Re: sgmlReader - why is my query finding no results

Posted 20 February 2013 - 05:56 AM

Ah, but is the <security> element attached to the root, or is it several levels down?
Was This Post Helpful? 0
  • +
  • -

#3 DaedalusAero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 04-July 12

Re: sgmlReader - why is my query finding no results

Posted 20 February 2013 - 06:02 AM

It's several levels down, Skydiver. The query actually needs to be able to find any element at any level, so in other words i need to be able to search for any element and the query should find it regarless of position. Can you give me a clue as to how i achieve this please?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3566
  • View blog
  • Posts: 11,082
  • Joined: 05-May 12

Re: sgmlReader - why is my query finding no results

Posted 20 February 2013 - 06:53 AM

Try using Descendants() instead of Elements().

http://msdn.microsof...escendants.aspx

It usually helps to read the documentation every now and then instead of depending on Intellisense.
Was This Post Helpful? 1
  • +
  • -

#5 DaedalusAero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 04-July 12

Re: sgmlReader - why is my query finding no results

Posted 20 February 2013 - 06:59 AM

Thanks, and Sorry i asked.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3566
  • View blog
  • Posts: 11,082
  • Joined: 05-May 12

Re: sgmlReader - why is my query finding no results

Posted 20 February 2013 - 07:12 AM

No problem asking.

I'm just trying to encourage people to take time to read documentation because there will be bits of information there that will either point you to a more efficient way of doing something, or warn you that using some classes/methods in some contexts are contraindicated (ex. using the Office Interop objects/classes within ASP.NET is highly discouraged).
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1