Skip to main content

Parsing XML - Quick 'n Dirty

LINQ can be used to dynamically and quickly parse XML.

The following data can be read in like so:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
    <root>
        <Element>
            <Name>.....</Name>
            <Height>.....</Height>
        </Element>
        <Element>
            <Name>.....</Name>
            <Height>.....</Height>
        </Element>
    </root>


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    XDocument doc = XDocument.Load("file.xml");
    var collection = from r in doc.Descendants("Element")
    select new
    {
        Name = r.Element("Name").Value,
        Height = r.Element("Height").Value,
    };

    foreach(var element in collection)
    {
        // Properties can be accessed like an object
        // element.Name;
        // element.Height;
    }

Comments

Popular posts from this blog

File menu opening on wrong side? [Windows 10]

If you are using windows 10 and the file menu opens on the left side, here is a fix: Run (Win+R) paste the following: shell:::{80F3F1D5-FECA-45F3-BC32-752C152E456E} click "Other" tab choose "Left-handed" Now the menu should open within the window:

Connecting VS 2008 to TFS?

Trouble connecting vs 2008 to TFS? If you are running a newer version of TFS, 2008 does not understand collections. In order to fix that, you need the following installed: SP1 http://www.microsoft.com/en-us/download/details.aspx?id=13276 Team Explorer http://www.microsoft.com/en-us/download/details.aspx?id=16338 Forwards Compatibility https://www.microsoft.com/en-us/download/details.aspx?id=10834

Replacing SystemColors with Hex value

If you're in a similar situation as me, using SystemColors is not an option, and you would rather use the real hex values. The following is a chart of all of the colors and their respective hex value: #FFB4B4B4 : ActiveBorderColor #FF99B4D1 : ActiveCaptionColor #FF000000 : ActiveCaptionTextColor #FFABABAB : AppWorkspaceColor #FFF0F0F0 : ControlColor #FFA0A0A0 : ControlDarkColor #FF696969 : ControlDarkDarkColor #FFE3E3E3 : ControlLightColor #FFFFFFFF : ControlLightLightColor #FF000000 : ControlTextColor #FF000000 : DesktopColor #FFB9D1EA : GradientActiveCaptionColor #FFD7E4F2 : GradientInactiveCaptionColor #FF6D6D6D : GrayTextColor #FF3399FF : HighlightColor #FFFFFFFF : H...