Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have followed many tutorials here on LINQ to XML http://www.dotnetcurry.com/showarticle.aspx?ID=564[^]

The terminology for the XML documents is confusing.
Could someone please help me to write the code snippet that would extract the "Name" value from this xml based on a condition for example.

I need colSDate and colAcqDate because Visible is True but not colSeqNo because it does not have the Visible property or it is set to False.



XML
<XtraSerializer version="1.0" application="View">
  <property name="#LayoutVersion" />
  <property name="ActiveFilterEnabled">true</property>
  <property name="Columns" iskey="true" value="286">
    <property name="Item1" isnull="true" iskey="true">
      <property name="Name">colSeqNo</property>
    </property>
    <property name="Item2" isnull="true" iskey="true">
      <property name="Visible">true</property>
      <property name="VisibleIndex">0</property>
      <property name="Name">colSDate</property>
    </property>
    <property name="Item3" isnull="true" iskey="true">
      <property name="Visible">true</property>
      <property name="VisibleIndex">1</property>
      <property name="Name">colAcqDate</property>
    </property>
  </property>
  <property name="ActiveFilterString" />
  <property name="GroupSummarySortInfoState" />
  <property name="FindFilterText" />
  <property name="FindPanelVisible">false</property>
</XtraSerializer>


I am trying to write some reasonably performing data access code but the absurd requirements are making it difficult. Any help is extremely appreciated. Thanks in advance.
Posted
Comments
Rob Philpott 30-May-14 12:25pm    
Why is every element called property??
RSIBrian2014 30-May-14 12:28pm    
That is part of what makes it difficult and something I have no control over. This is created from an XtraGrid and that is how they did it for some stupid reason.
Sergey Alexandrovich Kryukov 30-May-14 12:33pm    
Nothing difficult. It is a reasonable approach for some designs...
—SA
RSIBrian2014 30-May-14 12:36pm    
Any suggestions for extracting the data?
Sergey Alexandrovich Kryukov 30-May-14 16:59pm    
How about my answer? There is nothing difficult in "extracting" as well. You better parse, not "extract". Even better, use Data Contract.
—SA

You can use different approaches to XML parsing. Here is my short overview of them:

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the class System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].


Not always you need to work at XML level. Sometimes you can use available XML serialization. Please see:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

See also my past answers:
Creating property files...[^],
How can I utilize XML File streamwriter and reader in my form application?[^].

—SA
 
Share this answer
 
Comments
RSIBrian2014 30-May-14 12:45pm    
Thank you for your suggestions... However I have been doing quite a bit of research for 2 days and have failed a little more guidance would be very appreciated. Also it would be easy enough to write a parser from scratch to pull out those values but I don't expect that it will perform as well nor be a elegant or maintainable.
Rob Philpott 30-May-14 14:49pm    
I'll write you a bit of code tomorrow if you can wait that long?
RSIBrian2014 30-May-14 14:56pm    
I actually figured it out but thank you so much for being willing to do so.
Rob Philpott 30-May-14 15:08pm    
cool. :)
Sergey Alexandrovich Kryukov 30-May-14 17:00pm    
I don't know what guidance you may possibly need. Everything is in the classes references. Any particular problem?
And how about accepting the answer formally?
—SA
SQL
Dim result = From d In xml.Descendants
             Where d.Attribute("name").Value = "Visible" AndAlso
                   d.Value = "true"
             From e As XElement In d.Parent.Elements
             Where e.Attribute("name").Value = "Name"
             Select e.Value
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900