Skip to main content
GameDev.net gamedev.net
🔒 Locked

Chapter 17 XML + C#

Started by popcorn Aug 13, 2007 at 3:54 PM 1 replies 1.3k views
Original Post
popcorn
popcorn
At the start of chapter 17 the author mentions why properties have become one of the most important features in C# because of the importance of XML. But because I have limited experience in programming I am just taking him at his word and do not see the 'bigger picture' so I would like to know: 1. Why is XML so important? 2. As an example of the importance of XML the author talks about (p154): "using XML-based extensible markup language(XAML)is defining layout of windows and controls", why would you use XML for these purposes? 3. Why is C# such a good fit in conjunction with XML? Any examples would be nice with regards to this.
How about them apples?
Spoonbender
Spoonbender
Quote:
Original post by popcorn
1. Why is XML so important?

It's being used for an awful lot of things, so having good tools for manipulating XML documents is very handy. (XHTML documents are XML, OpenOffice and MS Office both use XML-based document formats, even the config files you can generate for your .NET project in Visual Studio is XML.

Quote:

2. As an example of the importance of XML the author talks about (p154): "using XML-based extensible markup language(XAML)is defining layout of windows and controls", why would you use XML for these purposes?

Short answer: "Because Microsoft says so". (Microsoft has defined XAML as a XML format for specifying this stuff)
Slightly longer answer:
A declarative language (where you just specify what you want, where, and how it should look) is a pretty good fit for describing layout. It's the same reason you use HTML for websites.
It's easier for non-programmers to design a layout, and it's easier to manipulate from, say, a C# program.

Quote:

3. Why is C# such a good fit in conjunction with XML?

Good question. I assume the answer he's looking for is "because C# has very good libraries for XML support"
The System.Xml namespace has a whole lot of handy utilities for creating, reading, modifying and transforming XML documents.
This isn't all that unique though (first, it's a property of the .NET framework, rather than C# itself, meaning all .NET languages have access to it, and second, there are tons of good XML tools outside .NET as well)
Still, it's true that if you want to mess with XML documents, C# probably isn't a bad language to use, since it comes with all the libraries necessary.
JWalsh
JWalsh
Spoonbender did a great job explaining, but I wanted to add a few things.

1. XML is important because it's a standards based format for declaring and describing arbitrary hierarchical data. All you need is a schema to validate your dataset by and poof! XHTML, XAML, SOAP, and any number of other topics are based on the popular XML standard.

2. XAML is a standards-based XML markup language for defining application layout and behavior. For the record, World of Warcraft also uses XML, though they do not call it XAML.

The idea is simple, UI is about 3 things - UI Elements, properties, and behaviors. The UI Elements are things such as Buttons, Sliders, TextBoxes, or even larger constructs such as Dialog Boxes and whole Windows. Using procedural methods such as regular C# code would require the allocation of several objects, then you'd have several lines of setting fields, and then attaching callbacks to event handlers. In code, this could be DOZENS lines long, and more or less requires a programmer to do it. However, with XAML it's much simpler. Even a non-programmer can edit an XML text file, which then gets processed by the XAML parser and the C# code to create and set the properties of the windows objects are generated by the XAML parser. Consider something like...
<Window Width="300" Height="200" Loaded="OnLoad">    <Grid>        <Grid.ColumnDefinitions>            <ColumnDefinition/>            <ColumnDefinition Width=25/>        </Grid.ColumnDefinitions>        <TextBox Grid.Column=0 VerticalAlignment="Center"/>        <Button Grid.Column=1 VerticalAlignment="Center" Click="OnClick" Content="Submit"/>    </Grid></Window>

The above would create a 300x200 window with two controls in it, a textbox, and a button. The textbox would be roughly 275 units wide, and the button 25 units wide. They'd be immediately next to each other. The button would have a label on it which reads "Submit" and there were two event handlers declared in the above "OnLoad" gets called when the window is Loaded and "OnClick" which gets called when the button is clicked. The above in WinForms would have been dozens and dozens of lines, both due to the creation of the UI elements, setting properties, as well as all the layout I did with the Grid.

3. The answer to your part 3 also lies in the above example, specifically, OnLoad and OnClick. You see, while XML is great at creating objects and setting properties, it is unsuited for processing behavior, such as a button click, a text change, a re-sizing of a window, etc...these must all be handling by some procedural process. In WPF, this is called Code-Behind, and can be implemented in any .NET compatible language, one of which is C#. So you use XML to define the layout and properties, and then you use C# to define what behaviors occur when the user interacts with the UI.

For the records, World of Warcraft uses Lua for it's Code-behind.

Cheers!
[/source]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.