<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>David Kline : XML</title><link>http://blogs.msdn.com/davidklinems/archive/tags/XML/default.aspx</link><description>Tags: XML</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Quick Tip: Using XPath to find nodes by attribute value</title><link>http://blogs.msdn.com/davidklinems/archive/2007/03/13/quick-tip-using-xpath-to-find-nodes-by-attribute-value.aspx</link><pubDate>Tue, 13 Mar 2007 21:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1874604</guid><dc:creator>DavidKlineMS</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/davidklinems/comments/1874604.aspx</comments><wfw:commentRss>http://blogs.msdn.com/davidklinems/commentrss.aspx?PostID=1874604</wfw:commentRss><description>&lt;P&gt;There are some things that I can just remember: phone numbers, locker combinations, and the like.&amp;nbsp; There are others that I have to lookup again and again and again.&amp;nbsp; XPath query syntax is one of the latter items.&amp;nbsp; Today, I'd like to talk a little about XPath queries.&lt;BR&gt;&lt;BR&gt;The following XML contains a collection of "Friend" nodes.&amp;nbsp; Each node has attributes for name and birth date.&amp;nbsp; To keep this post to a reasonable length, I am going to limit the XML to 5 nodes.&lt;BR&gt;&lt;BR&gt;&lt;CODE&gt;&amp;lt;MyFriends&amp;gt;&lt;BR&gt;&amp;nbsp; &amp;lt;Friend name="Sam" birthMonth="July"&amp;nbsp;birthDay="16" birthYear="1974" /&amp;gt;&lt;BR&gt;&amp;nbsp; &amp;lt;Friend name="Pam" birthMonth="April"&amp;nbsp;birthDay="7" birthYear="1967" /&amp;gt;&lt;BR&gt;&amp;nbsp; &amp;lt;Friend name="George" birthMonth="February"&amp;nbsp;birthDay="2" birthYear="1981" /&amp;gt;&lt;BR&gt;&amp;nbsp; &amp;lt;Friend name="John" birthMonth="April"&amp;nbsp;birthDay="11" birthYear="1972" /&amp;gt;&lt;BR&gt;&amp;nbsp; &amp;lt;Friend name="Martha" birthMonth="August"&amp;nbsp;birthDay="3" birthYear="1974" /&amp;gt;&lt;BR&gt;&amp;lt;/MyFriends&amp;gt;&lt;/CODE&gt;&lt;BR&gt;&lt;BR&gt;After loading this XML into an XmlDocument, I can use XPath to query for the nodes of interest.&lt;BR&gt;&lt;BR&gt;Since April is coming soon, I would like to get a list of my friends who has a birthday that month.&amp;nbsp; To do so, I query for Friend nodes where the value of the birthMonth attribute is "April".&lt;BR&gt;&lt;BR&gt;&lt;CODE&gt;// load the xml file into an XmlDocument&lt;BR&gt;XmlDocument doc = new XmlDocument();&lt;BR&gt;doc.Load("myfriends.xml");&lt;BR&gt;XmlElement rootNode = doc.DocumentElement;&lt;BR&gt;&lt;BR&gt;// select the friends who have a birthday in April&lt;BR&gt;String query = "Friend[@birthMonth='April']";&lt;BR&gt;XmlNodeList friends = rootNode.SelectNodes(query);&lt;BR&gt;&lt;BR&gt;foreach(XmlElement friend in friends)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // TODO: display a reminder to buy a birthday card&lt;BR&gt;}&lt;/CODE&gt;&lt;BR&gt;&lt;BR&gt;When the above snippet is run, two friends are returned: Pam and John.&amp;nbsp; The application can retrieve the desired information from the node and display an appropriate reminder.&lt;BR&gt;&lt;BR&gt;Enjoy!&lt;BR&gt;-- DK&lt;BR&gt;&lt;BR&gt;&lt;FONT size=1&gt;Edit: Fix grammar&lt;BR&gt;&lt;BR&gt;Disclaimer(s):&lt;BR&gt;This posting is provided "AS IS" with no warranties, and confers no rights.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1874604" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/davidklinems/archive/tags/Tips+and+Tricks/default.aspx">Tips and Tricks</category><category domain="http://blogs.msdn.com/davidklinems/archive/tags/XML/default.aspx">XML</category><category domain="http://blogs.msdn.com/davidklinems/archive/tags/Quick+Tips/default.aspx">Quick Tips</category></item><item><title>Quick Tip: Specifying a field's name when using the XmlSerializer</title><link>http://blogs.msdn.com/davidklinems/archive/2006/11/08/quick-tip-specifying-a-field-s-name-when-using-the-xmlserializer.aspx</link><pubDate>Wed, 08 Nov 2006 23:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1039693</guid><dc:creator>DavidKlineMS</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/davidklinems/comments/1039693.aspx</comments><wfw:commentRss>http://blogs.msdn.com/davidklinems/commentrss.aspx?PostID=1039693</wfw:commentRss><description>Last month, I wrote about how to &lt;A href="http://blogs.msdn.com/davidklinems/archive/2006/10/02/Quick-Tip_3A00_-Serializing-an-Object-Field-as-an-XML-Attribute.aspx" mce_href="http://blogs.msdn.com/davidklinems/archive/2006/10/02/Quick-Tip_3A00_-Serializing-an-Object-Field-as-an-XML-Attribute.aspx"&gt;instruct the XmlSerializer to create an XML attribute for fields in an object&lt;/A&gt;.&amp;nbsp; Today, I'd like to talk about how to specify the name used to represent a field in the resulting XML.&lt;BR&gt;&lt;BR&gt;By default, when an object is serialized, fields are serialized into nodes with names that match the name of the field.&amp;nbsp; When an instance of the example TestData object(below) is serialized&lt;BR&gt;&lt;BR&gt;&lt;CODE&gt;public class TestData&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Int32 TestID;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public String Description;&lt;BR&gt;}&lt;/CODE&gt;&lt;BR&gt;&lt;BR&gt;the TestID and Description fields become&lt;BR&gt;&lt;BR&gt;&lt;CODE&gt;&amp;lt;TestID&amp;gt;0&amp;lt;/TestID&amp;gt;&lt;BR&gt;&amp;lt;Description&amp;gt;Sample test description.&amp;lt;/Description&amp;gt;&lt;/CODE&gt;&lt;BR&gt;&lt;BR&gt;At times, it may be desirable to change the default naming behavior.&amp;nbsp; To change the name, we decorate the TestID field with an XmlElement attribute.&lt;BR&gt;&lt;BR&gt;&lt;CODE&gt;[XmlElement(ElementName="ID")]&lt;BR&gt;public Int32 TestID;&lt;/CODE&gt;&lt;BR&gt;&lt;BR&gt;After serialization, the XML contains&lt;BR&gt;&lt;BR&gt;&lt;CODE&gt;&amp;lt;ID&amp;gt;0&amp;lt;/ID&amp;gt;&lt;/CODE&gt;&lt;BR&gt;&lt;BR&gt;This same technique also works with the XmlAttribute attribute.&lt;BR&gt;&lt;BR&gt;&lt;CODE&gt;[XmlAttribute(AttributeName="ID")]&lt;BR&gt;public Int32 TestID;&lt;/CODE&gt;&lt;BR&gt;&lt;BR&gt;Creates an attribute called ID (highlighted in &lt;FONT color=#800080&gt;purple&lt;/FONT&gt;) in the TestData node.&lt;BR&gt;&lt;BR&gt;&lt;CODE&gt;&amp;lt;TestData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" &lt;FONT color=#800080&gt;ID="0"&lt;/FONT&gt;&amp;gt;&lt;/CODE&gt;&lt;BR&gt;&lt;BR&gt;Enjoy!,&lt;BR&gt;-- DK&lt;BR&gt;&lt;BR&gt;&lt;FONT size=1&gt;Disclaimers:&lt;BR&gt;This posting is provided "AS IS" with no warranties, and confers no rights.&lt;BR&gt;&amp;nbsp;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1039693" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/davidklinems/archive/tags/Tips+and+Tricks/default.aspx">Tips and Tricks</category><category domain="http://blogs.msdn.com/davidklinems/archive/tags/XML/default.aspx">XML</category><category domain="http://blogs.msdn.com/davidklinems/archive/tags/Quick+Tips/default.aspx">Quick Tips</category></item><item><title>Quick Tip: Serializing an Object Field as an XML Attribute</title><link>http://blogs.msdn.com/davidklinems/archive/2006/10/02/Quick-Tip_3A00_-Serializing-an-Object-Field-as-an-XML-Attribute.aspx</link><pubDate>Tue, 03 Oct 2006 00:55:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:782023</guid><dc:creator>DavidKlineMS</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/davidklinems/comments/782023.aspx</comments><wfw:commentRss>http://blogs.msdn.com/davidklinems/commentrss.aspx?PostID=782023</wfw:commentRss><description>The addition of the XML Serializer is one of the reasons I really love version 2 of the .NET Compact Framework.&amp;nbsp; I use the XML Serializer in very nearly every application I write; to save application state, data files, etc.&amp;nbsp; By default, the XML Serializer will create a child node for every field in a type.&amp;nbsp; For example, the following object&lt;BR&gt;&lt;BR&gt;&lt;CODE&gt;public class TestData&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Int32 TestID;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public String Description;&lt;BR&gt;}&lt;/CODE&gt;&lt;BR&gt;&lt;BR&gt;becomes&lt;BR&gt;&lt;BR&gt;&lt;CODE&gt;&amp;lt;TestData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&amp;gt;&lt;BR&gt;&amp;nbsp; &amp;lt;Description&amp;gt;Sample test description.&amp;lt;/Description&amp;gt;&lt;BR&gt;&amp;nbsp; &amp;lt;TestID&amp;gt;0&amp;lt;/TestID&amp;gt;&lt;BR&gt;&amp;lt;/TestData&amp;gt;&lt;/CODE&gt;&lt;BR&gt;&lt;BR&gt;There are times that you may wish to serialize one or more fields as attributes on the object's node (ex: reduce the size of the XML).&amp;nbsp; By decorating the TestID field with an &lt;A href="http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlattributeattribute.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlattributeattribute.aspx"&gt;XmlAttribute&lt;/A&gt; attribute, the TestID field&lt;BR&gt;&lt;BR&gt;&lt;CODE&gt;[XmlAttribute()]&lt;BR&gt;public Int32 TestID;&lt;/CODE&gt;&lt;BR&gt;&lt;BR&gt;now becomes an attribute (highlighted in &lt;FONT color=#800080&gt;purple&lt;/FONT&gt;) on the TestData node.&lt;BR&gt;&lt;BR&gt;&lt;CODE&gt;&amp;lt;TestData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" &lt;FONT color=#800080&gt;TestID="0"&lt;/FONT&gt;&amp;gt;&lt;BR&gt;&amp;nbsp; &amp;lt;Description&amp;gt;Sample test description.&amp;lt;/Description&amp;gt;&lt;BR&gt;&amp;lt;/TestData&amp;gt;&lt;/CODE&gt;&lt;BR&gt;&lt;BR&gt;Enjoy!&lt;BR&gt;-- DK&lt;BR&gt;&lt;BR&gt;&lt;FONT size=1&gt;Disclaimers:&lt;BR&gt;This posting is provided "AS IS" with no warranties, and confers no rights.&lt;BR&gt;&amp;nbsp;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=782023" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/davidklinems/archive/tags/Tips+and+Tricks/default.aspx">Tips and Tricks</category><category domain="http://blogs.msdn.com/davidklinems/archive/tags/XML/default.aspx">XML</category><category domain="http://blogs.msdn.com/davidklinems/archive/tags/Quick+Tips/default.aspx">Quick Tips</category></item><item><title>Simple object remoting using the XmlSerializer</title><link>http://blogs.msdn.com/davidklinems/archive/2004/10/27/248885.aspx</link><pubDate>Thu, 28 Oct 2004 04:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:248885</guid><dc:creator>DavidKlineMS</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/davidklinems/comments/248885.aspx</comments><wfw:commentRss>http://blogs.msdn.com/davidklinems/commentrss.aspx?PostID=248885</wfw:commentRss><description>&lt;p&gt;Today, I'm going to talk about how you can use the XmlSerializer, TcpClient and TcpListener classes to create a simple remoting layer to send your custom objects from one device to another.&amp;nbsp; This post requires the .NET Compact Framework version 2 (beta 1) or the .NET Framework for desktop PCs.&lt;/p&gt; &lt;p&gt;Since NetCF doesn't support the TCP Remoting classes, you will need to setup the network connection yourself.&amp;nbsp; The serializer handles all of the data transfer.&amp;nbsp; You can use this technique to send data between any combination of systems which support the XmlSerializer and network connectivity.&lt;/p&gt; &lt;p&gt;As you can see from the code below, there isn't too much to making a simple connection, though a real application will need to be more robust and will typically need to handle bi-directional data flow.&lt;/p&gt; &lt;p&gt;Let's take a look at simple client and server code that remotes a custom object between devices.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Object&lt;br /&gt;&lt;/strong&gt;The only requirements on the object is that it is serializable.&amp;nbsp; This means that it contains a nullary constructor and all of the public fields are either read/write or are a collection (supporting the Add method).&amp;nbsp; The example we will be using is a very simple object containing a name, a team name and a player's jersey number.&lt;br /&gt;&lt;code&gt;public class Player&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public String Name;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; pulbic String Team;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Int32 Number;&lt;br /&gt;}&lt;/p&gt;&lt;/code&gt; &lt;p&gt;&lt;strong&gt;Client&lt;/strong&gt;&lt;br /&gt;The client application instantiates the Player object and populates it's data fields.&lt;br /&gt;&lt;code&gt;Player p = new Player();&lt;br /&gt;p.Name = "David";&lt;br /&gt;p.Team = "NetCF";&lt;br /&gt;p.Number = 43;&lt;/p&gt;&lt;/code&gt; &lt;p&gt;Next, it creates an XmlSerializer object, informing the serializer of the type of data which will be sent.&lt;br /&gt;&lt;code&gt;XmlSerializer xs = new XmlSerializer(typeof(Player));&lt;/p&gt;&lt;/code&gt; &lt;p&gt;Once we have our serializer, we create a TcpClient object, get connected to the remote server and acquire the stream to be used to communicate with the server.&lt;br /&gt;&lt;code&gt;TcpClient client = new TcpClient();&lt;br /&gt;client.Connect(serverAddress, serverPort);&lt;br /&gt;Stream stream = client.GetStream();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/code&gt; &lt;br /&gt;Now, we can serialize our Player object.&amp;nbsp; Since the stream we provide the serializer is a network stream, the data is sent to the server when the serializer writes the XML.&lt;br /&gt;&lt;code&gt;xs.Serialize(stream, p);&lt;/p&gt;&lt;/code&gt; &lt;p&gt;The rest of our client is housekeeping.&amp;nbsp; We need to be sure to close our stream and client objects.&lt;br /&gt;&lt;code&gt;stream.Close();&lt;br /&gt;client.Close();&lt;/p&gt;&lt;/code&gt; &lt;p&gt;&lt;strong&gt;Server&lt;/strong&gt;&lt;br /&gt;Now let's&amp;nbsp;take a look at the server code.&lt;br /&gt;&lt;code&gt;Player p;&lt;/p&gt;&lt;/code&gt; &lt;p&gt;As with the client, we need to create an XmlSerializer object, informing the serializer of the type of data which will be received.&lt;br /&gt;&lt;code&gt;XmlSerializer xs = new XmlSerializer(typeof(Player));&lt;/p&gt;&lt;/code&gt; &lt;p&gt;Next, a listener needs to be created and started.&lt;br /&gt;&lt;code&gt;TcpListener listener = new TcpListener(port);&lt;br /&gt;listener.Start();&lt;/p&gt;&lt;/code&gt; &lt;p&gt;The listener accepts the incoming connection.&lt;br /&gt;&lt;code&gt;TcpClient client = listener.AcceptTcpClient();&lt;/p&gt;&lt;/code&gt; &lt;p&gt;And we can get the data stream from the client which was created by the call to AcceptTcpClient.&lt;br /&gt;&lt;code&gt;Stream stream = client.GetStream();&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Now we can deserialize the Player object sent by the remote client.&amp;nbsp; Like the client, the stream used for deserialization is a network stream.&amp;nbsp; This causes the data to be received when the serializer reads the XML.&lt;br /&gt;&lt;code&gt;p = (Player)xs.Deserialize(stream);&lt;/code&gt; &lt;/p&gt; &lt;p&gt;Again, we need to perform some housekeeping.&amp;nbsp; We need to be sure to close our stream, client and listener objects.&lt;br /&gt;&lt;code&gt;stream.Close();&lt;br /&gt;client.Close();&lt;br /&gt;listener.Close();&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;And we can display the received object.&lt;br /&gt;&lt;code&gt;MessageBox.Show(String.Format("Name: {0}\r\nTeam: {1}\r\nNumber: {2}",&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.Name, p.Team, p.Number));&lt;/p&gt;&lt;/code&gt; &lt;p&gt;As mentioned earlier, the above is a very simple example of how to remote a data object using the XmlSerializer.&amp;nbsp;&amp;nbsp;The networking code for real world applications will likely be significantly more involved and handle being sent and received by both devices.&lt;/p&gt; &lt;p&gt;Enjoy!&lt;br /&gt;-- DK&lt;/p&gt; &lt;p&gt;&lt;font size="1"&gt;[Edited to fix formatting]&lt;br /&gt;[Edited to fix typo]&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font size="1"&gt;Disclaimers:&lt;br /&gt;This posting is provided "AS IS" with no warranties, and confers no rights.&lt;br /&gt;Some of the information contained within this post may be in relation to beta software.&amp;nbsp; Any and all details are subject to change.&lt;br /&gt;&lt;/p&gt;&lt;/font&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=248885" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/davidklinems/archive/tags/Networking+_26002600_+Web+Services/default.aspx">Networking &amp;&amp; Web Services</category><category domain="http://blogs.msdn.com/davidklinems/archive/tags/Tips+and+Tricks/default.aspx">Tips and Tricks</category><category domain="http://blogs.msdn.com/davidklinems/archive/tags/XML/default.aspx">XML</category></item></channel></rss>