.NET and XML
March 9, 2010 by BPELworld.com
Product Description
If you’re seeking ways to build network-based applications or XML-based web services, Microsoft provides most of the tools you’ll need. XML is integrated into the .NET Framework and Visual Studio .NET, but if you want to get a grasp on how .NET and XML actually work together, that’s a different story. With .NET & XML, you can get under the hood to see how the .NET Framework implements XML, giving you the skills to write understandable XML-based code that interoperates with code written with other tools, and even other languages. .NET & XML starts by introducing XML and the .NET Framework, and then teaches you how to read and write XML before moving on to complex methods for manipulating, navigating, transforming, and constraining it. As you move from chapter to chapter, you’ll absorb increasingly complex information until you have enough knowledge to successfully program your own XML-based applications. This tutorial also contains a quick reference to the API, plus various useful appendices.
Buy from Amazon –> .NET and XML




i was looking for example on using the System.xml.serialize name space. This is the example copied from the book
public enum AddressType {
Home,
Office,
Billing,
Shipping,
Mailing,
Day,
Evening,
FAX
}
If you’ll look again at Example 9-7, you’ll see that each state is actually listed by its full name, not the abbreviation as listed in the State enumeration. Here I’ve added an XmlEnumAttribute for each state name. Note that I’ve skipped some in the interest of space:
public enum State {
[XmlEnum(Name="Alaska")]
AK,
[XmlEnum(Name="Alabama")]
AL,
[XmlEnum(Name="Arkansas")]
AR,
[XmlEnum(Name="Arizona")]
AZ,
// …
[XmlEnum(Name="Washington")]
WA,
[XmlEnum(Name="Wisconsin")]
WI,
[XmlEnum(Name="West Virginia")]
WV,
[XmlEnum(Name="Wyoming")]
WY
}
The Address class has one attribute, type, and four elements. Here I’ve added XmlAttributeAttribute and XmlElementAttribute, as appropriate. The AttributeName and ElementName fields of each attribute are used to set the names of the XML attributes and elements, respectively:
public class Address {
[XmlAttribute(AttributeName="type")]
public AddressType AddressType;
[XmlElement(ElementName="street")]
public string[ ] Street;
[XmlElement(ElementName="city")]
public string City;
[XmlElement(ElementName="state")]
public State State;
[XmlElement(ElementName="zip")]
public string Zip;
}
Similar to Address, the TelephoneNumber class has one attribute and three elements. Again, I’ve decorated each member with the appropriate attribute. Note also that here, as in Address, I’ve set the names of the attributes and elements to match the ones in the XML; that is, they all start with lowercase letters:
public class TelephoneNumber {
[XmlAttribute(AttributeName="type")]
public AddressType AddressType;
[XmlElement(ElementName="areacode")]
public string AreaCode;
[XmlElement(ElementName="exchange")]
public string Exchange;
[XmlElement(ElementName="number")]
public string Number;
}
Now we come to the meat of the personnel record, the Employee. This class has three attributes: firstname, middleinitial, and lastname, which I’ve treated with the appropriate attribute. However, the Employee class also has two additional elements, addresses and telephones. These two elements actually contain nested arrays of elements, so I’ve used the XmlArray and XmlArrayItem attributes to help the serializer figure out what to do with the XML elements it reads:
public class Employee {
[XmlAttribute(AttributeName="firstname")]
public string FirstName;
[XmlAttribute(AttributeName="middleinitial")]
public string MiddleInitial;
[XmlAttribute(AttributeName="lastname")]
public string LastName;
[XmlArray(ElementName="addresses")]
[XmlArrayItem(ElementName="address")]
public Address [ ] Addresses;
[XmlArray(ElementName="telephones")]
[XmlArrayItem(ElementName="telephone")]
public TelephoneNumber [ ] TelephoneNumbers;
[XmlAttribute(AttributeName="hiredate")]
public DateTime HireDate;
}
Here’s the document element, personnel, which is decorated with XmlRootAttribute. Although the Employees member is an array of Employee objects, it is not a nested array, like addresses and telephones. By adding the XmlElement attribute directly to the member, the XmlSerializer knows that this member is to be serialized as an array of employee elements, without a separate top-level element:
[XmlRoot(ElementName="personnel")]
public class Personnel {
[XmlElement(ElementName="employee")]
public Employee [ ] Employees;
}
Finally, I’ve made some changes to the Serializer class, which I introduced in Example 9-5. Serializer’s Main( ) method still uses the CreatePersonnel( ) to create some personnel records, but it then instantiates an XmlSerializer to deserialize the objects it created back out to a file:
public class Serializer {
public static void Main(string [ ] args) {
Personnel personnel = CreatePersonnel( );
XmlSerializer serializer = new XmlSerializer(typeof(Personnel));
using (FileStream stream = File.OpenWrite(”Personnel.xml”)) {
serializer.Serialize(stream,personnel);
}
}
}
notice its in java??? for crying out loud if your going to say .net and xml use .net examples not JAVA
Rating: 1 / 5
Note that I decided to write this review because I could not believe that so many people wrote so nice reviews about this book. My assumption is that someone or some people are really trying to sell this book, because this book is definitely not that good whatsoever.
Actually, this book is definitely not what I expected from a .NET/XML book. I was expecting a practical book with some theory, a good description about .NET’s XML implementation and good examples. This is not what this book included. I’m very flexible with books. It’s impossible that every book will be perfect. Actually, most aren’t, so you have to get used to it, but again this book is for the most part terrible, unless you only want to get a general idea of how to use XML with .NET, otherwise get a different book. Again, I don’t know where the reviews for this book came from, because this book is really not that good.
To name a couple of things that I find wrong with the book, lets start with the examples included. Two words: they suck! Each chapter is pretty much like this: here is the general theory, some of it unnecessary like the constant reference to W3C stuff; then, here are a few lame, simple examples without much substance; now, lets go to the next chapter.
The first time I looked inside the book I was looking for information about how .NET did Xml Validation, and it is just terrible. I actually found more information out of general .NET books I already had, than from this one. And when you are working on a project where you need this information, and you have a book that’s suppose to help you with this stuff, it is very disappointing to find out that the book is pretty much irrelevant.
You would assume validation is a very important XML topic, among many others, but there’s really not a lot of info on it. Actually, if you look on the book’s index, you’ll see that about half the related info is in the reference section, which you could get out of MSDN anyway. BTW, almost half of the book is simply reference for the different .NET XML namespaces. Again, the same data you could get out of MSDN.
In any case, I know there’s not much else to choose from, but pretty much anything else might be as good or possibly way better…
Rating: 2 / 5
This is a first edition book on the .net platform’s foundation Xml libraries. It was written more than five years ago, but still remains an excellent resource to anyone trying to get a handle on all those angle brackets (especially if you’ve just been thrown at a project with minimal exposure to Xml, and you think that learning LINQ sounds like wayyy too much work. Not that such a thing would ever happen in the software development world, of course). Bornstein’s writing is quick and engaging, and most importantly: helpful.
That being said, it is an older book, pre-dating even .net 2.0, so some of what is discussed is depricated material. Perhaps surprisingly, quite a bit of the material is still relevant in this modern .net 3.5 (soon 4.0) / Silverlight era.
I wish there was a second edition, so I could give five stars.
Rating: 4 / 5
Over the years, I’ve found it increasingly difficult to buy technology related books simply because of the speed in which they become obsolete. So now I look at each book as not only an instructional tome, but whether or not it will be useful 6 months down the road as a reference. This book (like many of O’Reilly’s titles) has easily earned a place in my library.
Mr. Bornstein’s method of writing seems to fit very well with the way I learn, and his coverage of the subject matter makes this book a great resource when I’m trying to remember the exact syntax of a specific method call.
Rating: 5 / 5
The advanced C# developer is the target audience for this book, although a Visual Basic.Net developer who is familar with C# will be able to follow the examples. All of the examples in this book are in C#. This book is sometimes very confusing especially starting in Chapter 5 – Manipulating XML with DOM. Some of the chapters must be read a couple of times in order to make sense and this book is somewhat difficult to read.
In Chapter 1 – Introduction to .Net and XML, Chapter 2 – Reading XML, Chapter 3 – Writing XML, and Chapter 4 – Reading and Writing Non-XML Formats establish a basic foundation. The more advanced subjects start in Chapter 5.
Here is a synopis of Chapters 5 through Chapter 11.
Chapter 5 – Manipulating XML with DOM
Chapter 6 – Navigating XML with XPath
Chapter 7 – Transforming XML with XPath
Chapter 8 – Constraining XML with Schemas
Chapter 9 – SOAP and XML Serialization
Chapter 10 – XML and Web Services
Chapter 11 – XML and Databases
The author provides a quick reference in chapters 13 through chapter 20 but needs an entire chapter (Chapter 12) to explain how to use the quick references.
Pros: The author covers just about every facet of XML in this book. The author provides 4 examples of using the memory stream for I-O. Other books that I have read just mention it briefly but did not provide examples. There are ample examples in this book.
Cons: This book is sometimes hard to read and can be confusing at times. You may have to read the chapters several times to make sense of the material. On page 12, the author states that “the simplest thing you can do with an existing XML document is to read it into memory”. Shouldn’t it be “read from memory” and “write to memory”? I may be wrong but this line confuses me everytime I read it. Like all O’Reilly books, the font used is very small and makes reading difficult.
Overall this is a good book for the intermediate to advanced C# developers who have a strong working knowledge of C# and .Net but for less experienced developers or Visual Basic developers with no knowledge of C#, this book may be confusing. This is definitely NOT a beginner book.
Rating: 4 / 5