Professional Documents
Culture Documents
Support Community
Search MSDN with Bing
Best Practices for
MSDN Library Representing XML
.NET Development
Articles and Overviews in the .NET Framework
XML and the .NET Framework
Extreme XML
Best Practices for Representing XML in the .NET Framework
Dare Obasanjo
Microsoft Corporation
Introduction
After a recent design review, a fellow PM asked if there are
any design guidelines for exposing XML in an API because
he'd seen lots of different approaches and he wasn't sure
which to choose. I told him I believed there were some
guidelines available on MSDN, but when I checked I only
came across an episode of MSDN TV entitled Passing XML
Data Inside the CLR, which does contain such information
but not in an easily consumable form. Thus the idea of
providing a printer-friendly version of Don Box's MSDN TV
episode, along with some of my own experiences working on
XML APIs at Microsoft thrown in, was created.
http://msdn.microsoft.com/en-us/library/ms950765.aspx Page 1 of 10
Best Practices for Representing XML in the .NET Framework 9/21/10 8:12 PM
http://msdn.microsoft.com/en-us/library/ms950765.aspx Page 2 of 10
Best Practices for Representing XML in the .NET Framework 9/21/10 8:12 PM
http://msdn.microsoft.com/en-us/library/ms950765.aspx Page 3 of 10
Best Practices for Representing XML in the .NET Framework 9/21/10 8:12 PM
http://msdn.microsoft.com/en-us/library/ms950765.aspx Page 4 of 10
Best Practices for Representing XML in the .NET Framework 9/21/10 8:12 PM
where such a property was set from XML resulting from the
SqlCommand.ExecuteXmlReader() method or the
XslTransform.Transform() method. In such cases, the user
would have to parse the document twice as shown in the
example below:
Copy
Email email = new Email();
email.From = "dareo@example.com";
email.To = "michealb@example.org";
email.Subject = "Hello World";
This gives users of the Email class a way to pass, set, and
retrieve XML data within the Body property in an efficient
manner when needed.
http://msdn.microsoft.com/en-us/library/ms950765.aspx Page 5 of 10
Best Practices for Representing XML in the .NET Framework 9/21/10 8:12 PM
XmlReader
XPathNavigator
http://msdn.microsoft.com/en-us/library/ms950765.aspx Page 6 of 10
Best Practices for Representing XML in the .NET Framework 9/21/10 8:12 PM
if((reader.NodeType == XmlNodeType.Ele
ment) && reader.Name.Equals("artist")){
artist = reader.ReadElementString();
title = reader.ReadElementString();
break;
}
}
Console.WriteLine("Artist={0}, Title={1}
", artist, title);
}
}
XPathNavigator:
Copy
public static void PrintArtistAndPrice(XPath
Navigator nav){
iterator.MoveNext();
Console.WriteLine("Artist={0}", iterator
.Current);
iterator.MoveNext();
Console.WriteLine("Title={0}", iterator.
Current);
}
http://msdn.microsoft.com/en-us/library/ms950765.aspx Page 7 of 10
Best Practices for Representing XML in the .NET Framework 9/21/10 8:12 PM
writer.WriteStartDocument();
writer.WriteStartElement("email");
writer.WriteStartElement("headers");
writer.WriteStartElement("header");
writer.WriteElementString("name", "to");
writer.WriteElementString("value", this.
To);
writer.WriteEndElement(); //header
writer.WriteStartElement("header");
writer.WriteElementString("name", "from"
);
writer.WriteElementString("value", this.
From);
writer.WriteEndElement(); //header
writer.WriteStartElement("header");
writer.WriteElementString("name", "subje
ct");
writer.WriteElementString("value", this.
Subject);
writer.WriteEndElement(); //header
http://msdn.microsoft.com/en-us/library/ms950765.aspx Page 8 of 10
Best Practices for Representing XML in the .NET Framework 9/21/10 8:12 PM
writer.WriteStartElement("header");
writer.WriteElementString("name", "sent"
);
writer.WriteElementString("value", XmlCo
nvert.ToString(this.Sent));
writer.WriteEndElement(); //header
writer.WriteEndElement(); //headers;
writer.WriteStartElement("body");
writer.WriteRaw(this.Body);
http://msdn.microsoft.com/en-us/library/ms950765.aspx Page 9 of 10
Best Practices for Representing XML in the .NET Framework 9/21/10 8:12 PM
Conclusion
In future versions of the .NET Framework, more emphasis
will be placed on cursor-based XML APIs like the
XPathNavigator exposed by the IXPathNavigable
interface. Such cursors will be the primary mechanisms for
interacting with XML in the .NET Framework.
http://msdn.microsoft.com/en-us/library/ms950765.aspx Page 10 of 10