SlideShare a Scribd company logo
Mohammad Shaker 
mohammadshaker.com 
@ZGTRShaker 
2011, 2012, 2013, 2014 
C# Advanced 
L03-XML & LINQ TO XML
XML
XML –The Definition
XML 
•HTML and XHTML? 
•XML 
–Extensible Markup Language 
–A metalanguagethat allows users to define their own customized markup languages, especially in order to display documents on the World Wide Web (WWW). 
•XSD 
–XML Schema Definition language 
•XDR 
–XML -Data Reduced schemas. 
•Tags
XML 
<contacts> 
<contactcontactId="2"> 
<firstName>Mohammad</firstName> 
<lastName>Shaker</lastName> 
</contact> 
<contactcontactId="3"> 
<firstName>Hamza</firstName> 
<lastName>Smith</lastName> 
</contact> 
</contacts>
XML 
<contacts> 
<contactcontactId="2"> 
<firstName>Mohammad</firstName> 
<lastName>Shaker</lastName> 
</contact> 
<contactcontactId="3"> 
<firstName>Hamza</firstName> 
<lastName>Smith</lastName> 
</contact> 
</contacts>
XML 
<contacts> 
<contactcontactId="2"> 
<firstName>Mohammad</firstName> 
<lastName>Shaker</lastName> 
</contact> 
<contactcontactId="3"> 
<firstName>Hamza</firstName> 
<lastName>Smith</lastName> 
</contact> 
</contacts> 
Contacts
XML 
<contacts> 
<contactcontactId="2"> 
<firstName>Mohammad</firstName> 
<lastName>Shaker</lastName> 
</contact> 
<contactcontactId="3"> 
<firstName>Hamza</firstName> 
<lastName>Smith</lastName> 
</contact> 
</contacts> 
Contacts
XML 
<contacts> 
<contactcontactId="2"> 
<firstName>Mohammad</firstName> 
<lastName>Shaker</lastName> 
</contact> 
<contactcontactId="3"> 
<firstName>Hamza</firstName> 
<lastName>Smith</lastName> 
</contact> 
</contacts> 
Contacts 
Contact 
Contact
XML 
<contacts> 
<contactcontactId="2"> 
<firstName>Mohammad</firstName> 
<lastName>Shaker</lastName> 
</contact> 
<contactcontactId="3"> 
<firstName>Hamza</firstName> 
<lastName>Smith</lastName> 
</contact> 
</contacts> 
Contacts 
Mohammad 
Shaker 
Hamza 
Smith
XML .NET Classes 
Class 
Description 
XmlNode 
Represents a single node in a document tree. It is the base of many of the 
classes shown in this chapter. If this node represents the root of an XML 
document, you can navigate to any position in the document from it. 
XmlDocument 
Extends the XmlNodeclass, but is often the first object you use when using 
XML. That’s because this class is used to load and save data from disk or 
elsewhere.
XML .NET Classes 
Class 
Description 
XmlElement 
Represents a single element in the XML document. XmlElementis derived 
from XmlLinkedNode, which in turn is derived from XmlNode. 
XmlAttribute 
Represents a single attribute. Like the XmlDocumentclass, it is derived from 
the XmlNodeclass. 
XmlText 
Represents the text between a starting tag and a closing tag. 
XmlComment 
Represents a special kind of node that is not regarded as part of the document 
other than to provide information to the reader about parts of the document. 
XmlNodeList 
Represents a collection of nodes.
Creating an XML Node in an XML FileProgrammatically
Creating XML Node 
static void Main(string[] args) 
{ 
XmlDocumentxmlDoc= new XmlDocument(); 
XmlNoderootNode= xmlDoc.CreateElement("users"); 
xmlDoc.AppendChild(rootNode); 
XmlNodeuserNode= xmlDoc.CreateElement("user"); 
XmlAttributeattribute = xmlDoc.CreateAttribute("age"); 
attribute.Value= "42"; 
userNode.Attributes.Append(attribute); 
userNode.InnerText= "John Doe"; 
rootNode.AppendChild(userNode); 
userNode= xmlDoc.CreateElement("user"); 
attribute = xmlDoc.CreateAttribute("age"); 
attribute.Value= "39"; 
userNode.Attributes.Append(attribute); 
userNode.InnerText= "Jane Doe"; 
rootNode.AppendChild(userNode); 
xmlDoc.Save("test-doc.xml"); 
} 
Create an XML File 
<users> 
<user age="42">John Doe</user> 
<user age="39">Jane Doe</user> 
</users>
Creating XML Node 
static void Main(string[] args) 
{ 
XmlDocumentxmlDoc= new XmlDocument(); 
XmlNoderootNode= xmlDoc.CreateElement("users"); 
xmlDoc.AppendChild(rootNode); 
XmlNodeuserNode= xmlDoc.CreateElement("user"); 
XmlAttributeattribute = xmlDoc.CreateAttribute("age"); 
attribute.Value= "42"; 
userNode.Attributes.Append(attribute); 
userNode.InnerText= "John Doe"; 
rootNode.AppendChild(userNode); 
userNode= xmlDoc.CreateElement("user"); 
attribute = xmlDoc.CreateAttribute("age"); 
attribute.Value= "39"; 
userNode.Attributes.Append(attribute); 
userNode.InnerText= "Jane Doe"; 
rootNode.AppendChild(userNode); 
xmlDoc.Save("test-doc.xml"); 
} 
Create an XML Node 
users 
<users> 
<user age="42">John Doe</user> 
<user age="39">Jane Doe</user> 
</users>
Creating XML Node 
static void Main(string[] args) 
{ 
XmlDocumentxmlDoc= new XmlDocument(); 
XmlNoderootNode= xmlDoc.CreateElement("users"); 
xmlDoc.AppendChild(rootNode); 
XmlNodeuserNode= xmlDoc.CreateElement("user"); 
XmlAttributeattribute = xmlDoc.CreateAttribute("age"); 
attribute.Value= "42"; 
userNode.Attributes.Append(attribute); 
userNode.InnerText= "John Doe"; 
rootNode.AppendChild(userNode); 
userNode= xmlDoc.CreateElement("user"); 
attribute = xmlDoc.CreateAttribute("age"); 
attribute.Value= "39"; 
userNode.Attributes.Append(attribute); 
userNode.InnerText= "Jane Doe"; 
rootNode.AppendChild(userNode); 
xmlDoc.Save("test-doc.xml"); 
} 
Append the XML Node to the XML File 
<users> 
<user age="42">John Doe</user> 
<user age="39">Jane Doe</user> 
</users>
Creating XML Node 
static void Main(string[] args) 
{ 
XmlDocumentxmlDoc= new XmlDocument(); 
XmlNoderootNode= xmlDoc.CreateElement("users"); 
xmlDoc.AppendChild(rootNode); 
XmlNodeuserNode= xmlDoc.CreateElement("user"); 
XmlAttributeattribute = xmlDoc.CreateAttribute("age"); 
attribute.Value= "42"; 
userNode.Attributes.Append(attribute); 
userNode.InnerText= "John Doe"; 
rootNode.AppendChild(userNode); 
userNode= xmlDoc.CreateElement("user"); 
attribute = xmlDoc.CreateAttribute("age"); 
attribute.Value= "39"; 
userNode.Attributes.Append(attribute); 
userNode.InnerText= "Jane Doe"; 
rootNode.AppendChild(userNode); 
xmlDoc.Save("test-doc.xml"); 
} 
Create an XML Node 
user 
<users> 
<user age="42">John Doe</user> 
<user age="39">Jane Doe</user> 
</users>
Creating XML Node 
static void Main(string[] args) 
{ 
XmlDocumentxmlDoc= new XmlDocument(); 
XmlNoderootNode= xmlDoc.CreateElement("users"); 
xmlDoc.AppendChild(rootNode); 
XmlNodeuserNode= xmlDoc.CreateElement("user"); 
XmlAttributeattribute = xmlDoc.CreateAttribute("age"); 
attribute.Value= "42"; 
userNode.Attributes.Append(attribute); 
userNode.InnerText= "John Doe"; 
rootNode.AppendChild(userNode); 
userNode= xmlDoc.CreateElement("user"); 
attribute = xmlDoc.CreateAttribute("age"); 
attribute.Value= "39"; 
userNode.Attributes.Append(attribute); 
userNode.InnerText= "Jane Doe"; 
rootNode.AppendChild(userNode); 
xmlDoc.Save("test-doc.xml"); 
} 
Append the node: user to the root node: users 
<users> 
<user age="42">John Doe</user> 
<user age="39">Jane Doe</user> 
</users>
Creating XML Node 
static void Main(string[] args) 
{ 
XmlDocumentxmlDoc= new XmlDocument(); 
XmlNoderootNode= xmlDoc.CreateElement("users"); 
xmlDoc.AppendChild(rootNode); 
XmlNodeuserNode= xmlDoc.CreateElement("user"); 
XmlAttributeattribute = xmlDoc.CreateAttribute("age"); 
attribute.Value= "42"; 
userNode.Attributes.Append(attribute); 
userNode.InnerText= "John Doe"; 
rootNode.AppendChild(userNode); 
userNode= xmlDoc.CreateElement("user"); 
attribute = xmlDoc.CreateAttribute("age"); 
attribute.Value= "39"; 
userNode.Attributes.Append(attribute); 
userNode.InnerText= "Jane Doe"; 
rootNode.AppendChild(userNode); 
xmlDoc.Save("test-doc.xml"); 
} 
Create another XML Node user 
<users> 
<user age="42">John Doe</user> 
<user age="39">Jane Doe</user> 
</users>
Creating XML Node 
static void Main(string[] args) 
{ 
XmlDocumentxmlDoc= new XmlDocument(); 
XmlNoderootNode= xmlDoc.CreateElement("users"); 
xmlDoc.AppendChild(rootNode); 
XmlNodeuserNode= xmlDoc.CreateElement("user"); 
XmlAttributeattribute = xmlDoc.CreateAttribute("age"); 
attribute.Value= "42"; 
userNode.Attributes.Append(attribute); 
userNode.InnerText= "John Doe"; 
rootNode.AppendChild(userNode); 
userNode= xmlDoc.CreateElement("user"); 
attribute = xmlDoc.CreateAttribute("age"); 
attribute.Value= "39"; 
userNode.Attributes.Append(attribute); 
userNode.InnerText= "Jane Doe"; 
rootNode.AppendChild(userNode); 
xmlDoc.Save("test-doc.xml"); 
} 
Save the file where you want 
<users> 
<user age="42">John Doe</user> 
<user age="39">Jane Doe</user> 
</users>
Building a Node -A Faster Way? 
XElementxml=newXElement("contacts", 
newXElement("contact", 
newXAttribute("contactId", "2"), 
newXElement("firstName", "Mohammad"), 
newXElement("lastName", "Shaker") 
), 
newXElement("contact", 
newXAttribute("contactId", "3"), 
newXElement("firstName", "Hamza"), 
newXElement("lastName", "Smith") 
) 
); 
Console.WriteLine(xml);
Building a Node -A Faster Way? 
XElementxml=newXElement("contacts", 
newXElement("contact", 
newXAttribute("contactId", "2"), 
newXElement("firstName", "Mohammad"), 
newXElement("lastName", "Shaker") 
), 
newXElement("contact", 
newXAttribute("contactId", "3"), 
newXElement("firstName", "Hamza"), 
newXElement("lastName", "Smith") 
) 
); 
Console.WriteLine(xml); 
<contacts> 
<contactcontactId="2"> 
<firstName>Mohammad</firstName> 
<lastName>Shaker</lastName> 
</contact> 
<contactcontactId="3"> 
<firstName>Hamza</firstName> 
<lastName>Smith</lastName> 
</contact> 
</contacts>
Searching an XML
XML 
•Selecting Nodes
XML 
•Selecting Nodes
XML 
•Selecting Nodes
Getting XML Node Values
XML 
<?xml version="1.0" encoding="utf-8"?> 
<RoomsCoordProperties> 
<LocationDimension> 10 </LocationDimension> 
<Number> 5 </Number> 
<Width> 12 </Width> 
<Length> 12 </Length> 
<Height> 3 </Height> 
<Opacity> 0.3 </Opacity> 
<BasicLocationLength> 4 </BasicLocationLength> 
<Room id="3"> 
<LocationID> 0 </LocationID> 
<Width> 12 </Width> 
<Length> 12 </Length> 
<Height> 3 </Height> 
<Opacity> 0.3 </Opacity> 
</Room> 
… 
public static void InitializeRoomFromXMLFile(Room myRoom, introomNumber) 
{ 
XmlDocumentxmlDoc= new XmlDocument(); 
xmlDoc.Load(XMLFilePath); 
XmlNodeListxmlNodeList= xmlDoc.GetElementsByTagName("Room"); 
myRoom.RoomID= Int32.Parse(xmlNodeList[roomNumber].Attributes[0].FirstChild.Value); 
myRoom.RoomLocationID= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[0].FirstChild.Value); 
myRoom.RoomWidth= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[1].FirstChild.Value); 
myRoom.RoomLength= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[2].FirstChild.Value); 
myRoom.RoomHeight= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[3].FirstChild.Value); 
myRoom.RoomOpacity= Double.Parse(xmlNodeList[roomNumber].ChildNodes[4].FirstChild.Value); 
}
XML 
<?xml version="1.0" encoding="utf-8"?> 
<RoomsCoordProperties> 
<LocationDimension> 10 </LocationDimension> 
<Number> 5 </Number> 
<Width> 12 </Width> 
<Length> 12 </Length> 
<Height> 3 </Height> 
<Opacity> 0.3 </Opacity> 
<BasicLocationLength> 4 </BasicLocationLength> 
<Room id="3"> 
<LocationID> 0 </LocationID> 
<Width> 12 </Width> 
<Length> 12 </Length> 
<Height> 3 </Height> 
<Opacity> 0.3 </Opacity> 
</Room> 
… 
public static void InitializeRoomFromXMLFile(Room myRoom, introomNumber) 
{ 
XmlDocumentxmlDoc= new XmlDocument(); 
xmlDoc.Load(XMLFilePath); 
XmlNodeListxmlNodeList= xmlDoc.GetElementsByTagName("Room"); 
myRoom.RoomID= Int32.Parse(xmlNodeList[roomNumber].Attributes[0].FirstChild.Value); 
myRoom.RoomLocationID= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[0].FirstChild.Value); 
myRoom.RoomWidth= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[1].FirstChild.Value); 
myRoom.RoomLength= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[2].FirstChild.Value); 
myRoom.RoomHeight= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[3].FirstChild.Value); 
myRoom.RoomOpacity= Double.Parse(xmlNodeList[roomNumber].ChildNodes[4].FirstChild.Value); 
}
XML 
<?xml version="1.0" encoding="utf-8"?> 
<RoomsCoordProperties> 
<LocationDimension> 10 </LocationDimension> 
<Number> 5 </Number> 
<Width> 12 </Width> 
<Length> 12 </Length> 
<Height> 3 </Height> 
<Opacity> 0.3 </Opacity> 
<BasicLocationLength> 4 </BasicLocationLength> 
<Room id="3"> 
<LocationID> 0 </LocationID> 
<Width> 12 </Width> 
<Length> 12 </Length> 
<Height> 3 </Height> 
<Opacity> 0.3 </Opacity> 
</Room> 
… 
public static void InitializeRoomFromXMLFile(Room myRoom, introomNumber) 
{ 
XmlDocumentxmlDoc= new XmlDocument(); 
xmlDoc.Load(XMLFilePath); 
XmlNodeListxmlNodeList= xmlDoc.GetElementsByTagName("Room"); 
myRoom.RoomID= Int32.Parse(xmlNodeList[roomNumber].Attributes[0].FirstChild.Value); 
myRoom.RoomLocationID= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[0].FirstChild.Value); 
myRoom.RoomWidth= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[1].FirstChild.Value); 
myRoom.RoomLength= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[2].FirstChild.Value); 
myRoom.RoomHeight= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[3].FirstChild.Value); 
myRoom.RoomOpacity= Double.Parse(xmlNodeList[roomNumber].ChildNodes[4].FirstChild.Value); 
}
XML 
<?xml version="1.0" encoding="utf-8"?> 
<RoomsCoordProperties> 
<LocationDimension> 10 </LocationDimension> 
<Number> 5 </Number> 
<Width> 12 </Width> 
<Length> 12 </Length> 
<Height> 3 </Height> 
<Opacity> 0.3 </Opacity> 
<BasicLocationLength> 4 </BasicLocationLength> 
<Room id="3"> 
<LocationID> 0 </LocationID> 
<Width> 12 </Width> 
<Length> 12 </Length> 
<Height> 3 </Height> 
<Opacity> 0.3 </Opacity> 
</Room> 
… 
public static void InitializeRoomFromXMLFile(Room myRoom, introomNumber) 
{ 
XmlDocumentxmlDoc= new XmlDocument(); 
xmlDoc.Load(XMLFilePath); 
XmlNodeListxmlNodeList= xmlDoc.GetElementsByTagName("Room"); 
myRoom.RoomID= Int32.Parse(xmlNodeList[roomNumber].Attributes[0].FirstChild.Value); 
myRoom.RoomLocationID= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[0].FirstChild.Value); 
myRoom.RoomWidth= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[1].FirstChild.Value); 
myRoom.RoomLength= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[2].FirstChild.Value); 
myRoom.RoomHeight= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[3].FirstChild.Value); 
myRoom.RoomOpacity= Double.Parse(xmlNodeList[roomNumber].ChildNodes[4].FirstChild.Value); 
}
LINQ to XMLXML to LINQ
LINQ and XML 
•LINQ to XML 
•XML to LINQ 
varquery = from p in people 
where p.CanCode 
select new Xelement(“Person”, new Xattribute(“Age”, p.Age), p.Name) 
varx = new XElement(“People”, 
from p in people 
where p.CanCode 
select new Xelement(“Person”, new Xattribute(“Age”, p.Age), p.Name) 
)
LINQ to XML 
•Let’s have the following XML file 
<customers> 
<customerid="84"> 
<namevalue="Sample Name"/> 
</customer> 
<customerid="89"> 
<namevalue="Sample Name 2"/> 
</customer> 
<customerid="80"> 
<namevalue="Sample Name 3"/> 
</customer> 
</customers>
LINQ to XML 
XmlNodesearched=null; 
XmlDocumentdoc=newXmlDocument(); 
doc.Load(@"D:Temporarycustomers.xml"); 
foreach(XmlNodenodeindoc.SelectNodes("/customers/customer")) 
{ 
if(node.Attributes["id"].InnerText=="80") 
{ 
searched=node; 
break; 
} 
}
LINQ to XML 
XmlNodesearched=null; 
XmlDocumentdoc=newXmlDocument(); 
doc.Load(@"D:Temporarycustomers.xml"); 
foreach(XmlNodenodeindoc.SelectNodes("/customers/customer")) 
{ 
if(node.Attributes["id"].InnerText=="80") 
{ 
searched=node; 
break; 
} 
} 
XElementmain=XElement.Load(@"D:Temporarycustomers.xml"); 
IEnumerable<XElement>searched= 
fromcinmain.Elements("customer") 
where(string)c.Attribute("id") =="80" 
selectc;
LINQ to XML 
XmlNodesearched=null; 
XmlDocumentdoc=newXmlDocument(); 
doc.Load(@"D:Temporarycustomers.xml"); 
foreach(XmlNodenodeindoc.SelectNodes("/customers/customer")) 
{ 
if(node.Attributes["id"].InnerText=="80") 
{ 
searched=node; 
break; 
} 
} 
XElementmain=XElement.Load(@"D:Temporarycustomers.xml"); 
IEnumerable<XElement>searched= 
fromcinmain.Elements("customer") 
where(string)c.Attribute("id") =="80" 
selectc; 
<customers> 
<customerid="84"> 
<namevalue="Sample Name"/> 
</customer> 
<customerid="89"> 
<namevalue="Sample Name 2"/> 
</customer> 
<customerid="80"> 
<namevalue="Sample Name 3"/> 
</customer> 
</customers>
LINQ to XML 
XmlNodesearched=null; 
XmlDocumentdoc=newXmlDocument(); 
doc.Load(@"D:Temporarycustomers.xml"); 
foreach(XmlNodenodeindoc.SelectNodes("/customers/customer")) 
{ 
if(node.Attributes["id"].InnerText=="80") 
{ 
searched=node; 
break; 
} 
} 
XElementmain=XElement.Load(@"D:Temporarycustomers.xml"); 
IEnumerable<XElement>searched= 
fromcinmain.Elements("customer") 
where(string)c.Attribute("id") =="80" 
selectc; 
<customerid="80"> 
<namevalue="Sample Name 3"/> 
</customer> 
<customers> 
<customerid="84"> 
<namevalue="Sample Name"/> 
</customer> 
<customerid="89"> 
<namevalue="Sample Name 2"/> 
</customer> 
<customerid="80"> 
<namevalue="Sample Name 3"/> 
</customer> 
</customers>
LINQ to XML 
XElementmain=XElement.Load(@"D:Temporarycustomers.xml"); 
IEnumerable<XElement>searched= 
fromcinmain.Elements("customer") 
where(string)c.Element("name").Attribute("value") =="Sample Name" 
selectc; 
<customers> 
<customerid="84"> 
<namevalue="Sample Name"/> 
</customer> 
<customerid="89"> 
<namevalue="Sample Name 2"/> 
</customer> 
<customerid="80"> 
<namevalue="Sample Name 3"/> 
</customer> 
</customers>
LINQ to XML 
XElementmain=XElement.Load(@"D:Temporarycustomers.xml"); 
IEnumerable<XElement>searched= 
fromcinmain.Elements("customer") 
where(string)c.Element("name").Attribute("value") =="Sample Name" 
selectc; 
<customerid="84"> 
<namevalue="Sample Name"/> 
</customer> 
<customers> 
<customerid="84"> 
<namevalue="Sample Name"/> 
</customer> 
<customerid="89"> 
<namevalue="Sample Name 2"/> 
</customer> 
<customerid="80"> 
<namevalue="Sample Name 3"/> 
</customer> 
</customers>
LINQ to XML 
XElementmain=XElement.Load(@"D:Temporarycustomers.xml"); 
IEnumerable<XElement>searched= 
fromcinmain.Elements("customer") 
where(string)c.Attribute("id") =="84" 
&&(string)c.Element("name").Attribute("value") =="Sample Name" 
selectc; 
<customers> 
<customerid="84"> 
<namevalue="Sample Name"/> 
</customer> 
<customerid="89"> 
<namevalue="Sample Name 2"/> 
</customer> 
<customerid="80"> 
<namevalue="Sample Name 3"/> 
</customer> 
</customers>
LINQ to XML 
XElementmain=XElement.Load(@"D:Temporarycustomers.xml"); 
IEnumerable<XElement>searched= 
fromcinmain.Elements("customer") 
where(string)c.Attribute("id") =="84" 
&&(string)c.Element("name").Attribute("value") =="Sample Name" 
selectc; 
<customerid="84"> 
<namevalue="Sample Name"/> 
</customer> 
<customers> 
<customerid="84"> 
<namevalue="Sample Name"/> 
</customer> 
<customerid="89"> 
<namevalue="Sample Name 2"/> 
</customer> 
<customerid="80"> 
<namevalue="Sample Name 3"/> 
</customer> 
</customers>
Performanceof LINQ 
•LINQ has more control and efficiency in O/R Mapping than NHibernate 
–LINQ: ExternlMapping or Attribute Mapping 
–NHibernate: ExternlMapping 
•Because of mapping, LINQ is lower than database tools such as SqlDataReaderor SqlDataAdapter 
–In large dataset, their performance are more and more similar
XML Serialization
XML SerializationNot Always an Easy, Straightforward Task
XML SerializationWhy to?
XML SerializationSerialize a class that simply consists of public fields and properties into an XML file
XML SerializationUse XML serialization to generate an XML stream that conforms to a specific XML Schema (XSD) document.
Test CaseSerializing a Class
XML Serialization 
namespace XMLTest1 
{ 
public class Test 
{ 
public String value1; 
public String value2; 
} 
class Program 
{ 
static void Main(string[] args) 
{ 
Test myTest= new Test() { value1 = "Value 1", value2 = "Value 2" }; 
XmlSerializerx = new XmlSerializer(myTest.GetType()); 
x.Serialize(Console.Out, myTest); 
Console.ReadKey(); 
} 
} 
}
XML Serialization 
namespace XMLTest1 
{ 
public class Test 
{ 
public String value1; 
public String value2; 
} 
class Program 
{ 
static void Main(string[] args) 
{ 
Test myTest= new Test() { value1 = "Value 1", value2 = "Value 2" }; 
XmlSerializerx = new XmlSerializer(myTest.GetType()); 
x.Serialize(Console.Out, myTest); 
Console.ReadKey(); 
} 
} 
} 
<?xml version="1.0" encoding="ibm850"?> 
<Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<value1>Value 1</value1> 
<value2>Value 2</value2> 
</Test> 
That’s Cool!
Test CaseDeSerializinga Class
XML DeSerialization 
namespace XMLTest1 
{ 
public class Test 
{ 
public String value1; 
public String value2; 
} 
class Program 
{ 
static void Main(string[] args) 
{ 
String xData= "<?xml version="1.0" encoding="ibm850"?><Test xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><value1>Value 1</value1><value2>Value 2</value2></Test>"; 
XmlSerializerx = new XmlSerializer(typeof(Test)); 
Test myTest= (Test)x.Deserialize(new StringReader(xData)); 
Console.WriteLine("V1: " + myTest.value1); 
Console.WriteLine("V2: " + myTest.value2); 
Console.ReadKey(); 
} 
} 
}
XML DeSerialization 
namespace XMLTest1 
{ 
public class Test 
{ 
public String value1; 
public String value2; 
} 
class Program 
{ 
static void Main(string[] args) 
{ 
String xData= "<?xml version="1.0" encoding="ibm850"?><Test xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><value1>Value 1</value1><value2>Value 2</value2></Test>"; 
XmlSerializerx = new XmlSerializer(typeof(Test)); 
Test myTest= (Test)x.Deserialize(new StringReader(xData)); 
Console.WriteLine("V1: " + myTest.value1); 
Console.WriteLine("V2: " + myTest.value2); 
Console.ReadKey(); 
} 
} 
}
XML Serialization and Attributes
XML Serialization and Attributes 
[XmlRoot("XTest")] 
public class Test 
{ 
[XmlElement(ElementName="V1")] 
public String value1; 
[XmlElement(ElementName="V2")] 
public String value2; 
[XmlArray("OtherValues")] 
[XmlArrayItem("OValue")] 
public List others = new List(); 
}
XML Serialization and Attributes 
[XmlRoot("XTest")] 
public class Test 
{ 
[XmlElement(ElementName="V1")] 
public String value1; 
[XmlElement(ElementName="V2")] 
public String value2; 
[XmlArray("OtherValues")] 
[XmlArrayItem("OValue")] 
public List others = new List(); 
} 
<?xml version="1.0" encoding="ibm850"?> 
<XTestxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<V1>A1</V1> 
<V2>B1</V2> 
<OtherValues> 
<OValue>Test</OValue> 
</OtherValues> 
</XTest>
XML Serialization and Attributes 
[XmlRoot("XTest")] 
public class Test 
{ 
[XmlElement(ElementName="V1")] 
public String value1; 
[XmlElement(ElementName="V2")] 
public String value2; 
[XmlArray("OtherValues")] 
[XmlArrayItem("OValue")] 
public List others = new List(); 
} 
<?xml version="1.0" encoding="ibm850"?> 
<XTestxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<V1>A1</V1> 
<V2>B1</V2> 
<OtherValues> 
<OValue>Test</OValue> 
</OtherValues> 
</XTest>
XML Serialization and Attributes 
[XmlRoot("XTest")] 
public class Test 
{ 
[XmlElement(ElementName="V1")] 
public String value1; 
[XmlElement(ElementName="V2")] 
public String value2; 
[XmlArray("OtherValues")] 
[XmlArrayItem("OValue")] 
public List others = new List(); 
} 
<?xml version="1.0" encoding="ibm850"?> 
<XTestxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<V1>A1</V1> 
<V2>B1</V2> 
<OtherValues> 
<OValue>Test</OValue> 
</OtherValues> 
</XTest>
XML Serialization and Attributes 
[XmlRoot("XTest")] 
public class Test 
{ 
[XmlElement(ElementName="V1")] 
public String value1; 
[XmlElement(ElementName="V2")] 
public String value2; 
[XmlArray("OtherValues")] 
[XmlArrayItem("OValue")] 
public List others = new List(); 
} 
<?xml version="1.0" encoding="ibm850"?> 
<XTestxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<V1>A1</V1> 
<V2>B1</V2> 
<OtherValues> 
<OValue>Test</OValue> 
</OtherValues> 
</XTest>
XML Serialization and Attributes 
[XmlRoot("XTest")] 
public class Test 
{ 
[XmlElement(ElementName="V1")] 
public String value1; 
[XmlElement(ElementName="V2")] 
public String value2; 
[XmlArray("OtherValues")] 
[XmlArrayItem("OValue")] 
public List others = new List(); 
} 
<?xml version="1.0" encoding="ibm850"?> 
<XTestxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<V1>A1</V1> 
<V2>B1</V2> 
<OtherValues> 
<OValue>Test</OValue> 
</OtherValues> 
</XTest>
XML Documentation
XML DocumentationC# provides a mechanism for developers to document their code using XML. In source code files, lines that begin with ///and that precede a user-defined type such as a class, delegate, or interface; a member such as a field, event, property, or method; or a namespace declaration can be processed as comments and placed in a file.
XML Documentation 
/// <summary> 
/// Class level summary documentation goes here.</summary> 
/// <remarks> 
/// Longer comments can be associated with a type or member 
/// through the remarks tag</remarks> 
public class SomeClass 
{ 
/// <summary> 
/// Store for the name property</summary> 
private string myName= null; 
/// <summary> 
/// The class constructor. </summary> 
public SomeClass() 
{ 
// TODO: Add Constructor Logic here 
} 
/// <summary> 
/// Name property </summary> 
/// <value> 
/// A value tag is used to describe the property value</value> 
public string Name 
{ 
get 
{ 
return myName; 
} 
} 
/// <summary> 
/// Description for SomeMethod.</summary> 
/// <paramname="s"> Parameter description for s goes here</param> 
/// <seealsocref="String"> 
/// You can use the crefattribute on any tag to reference a type or member 
/// and the compiler will check that the reference exists. </seealso> 
public void SomeMethod(string s) { }
XML Documentation 
/// <summary> 
/// Class level summary documentation goes here.</summary> 
/// <remarks> 
/// Longer comments can be associated with a type or member 
/// through the remarks tag</remarks> 
public class SomeClass 
{ 
/// <summary> 
/// Store for the name property</summary> 
private string myName= null; 
/// <summary> 
/// The class constructor. </summary> 
public SomeClass() 
{ 
// TODO: Add Constructor Logic here 
} 
/// <summary> 
/// Name property </summary> 
/// <value> 
/// A value tag is used to describe the property value</value> 
public string Name 
{ 
get 
{ 
return myName; 
} 
} 
/// <summary> 
/// Description for SomeMethod.</summary> 
/// <paramname="s"> Parameter description for s goes here</param> 
/// <seealsocref="String"> 
/// You can use the crefattribute on any tag to reference a type or member 
/// and the compiler will check that the reference exists. </seealso> 
public void SomeMethod(string s) { }
XML Documentation 
/// <summary> 
/// Class level summary documentation goes here.</summary> 
/// <remarks> 
/// Longer comments can be associated with a type or member 
/// through the remarks tag</remarks> 
public class SomeClass 
{ 
/// <summary> 
/// Store for the name property</summary> 
private string myName= null; 
/// <summary> 
/// The class constructor. </summary> 
public SomeClass() 
{ 
// TODO: Add Constructor Logic here 
} 
/// <summary> 
/// Name property </summary> 
/// <value> 
/// A value tag is used to describe the property value</value> 
public string Name 
{ 
get 
{ 
return myName; 
} 
} 
/// <summary> 
/// Description for SomeMethod.</summary> 
/// <paramname="s"> Parameter description for s goes here</param> 
/// <seealsocref="String"> 
/// You can use the crefattribute on any tag to reference a type or member 
/// and the compiler will check that the reference exists. </seealso> 
public void SomeMethod(string s) { } 
<?xml version="1.0"?> 
<doc> 
<assembly> 
<name>xmlsample</name> 
</assembly> 
<members> 
<member name="T:SomeClass"> 
<summary> 
Class level summary documentation goes here. 
</summary> 
<remarks> 
Longer comments can be associated with a type or member 
through the remarks tag 
</remarks> 
</member> 
<member name="F:SomeClass.myName"> 
<summary> 
Store for the name property 
</summary> 
</member> 
<member name="M:SomeClass.#ctor"> 
<summary>The class constructor.</summary> 
</member> 
<member name="M:SomeClass.SomeMethod(System.String)"> 
<summary>Description for SomeMethod.</summary> 
<paramname="s"> Parameter description for s goes here</param> 
<seealsocref="T:System.String"> 
You can use the crefattribute on any tag to reference a type or member 
and the compiler will check that the reference exists. </seealso> 
</member> 
…. 
</doc>
XML Documentation 
/// <summary> 
/// Class level summary documentation goes here.</summary> 
/// <remarks> 
/// Longer comments can be associated with a type or member 
/// through the remarks tag</remarks> 
public class SomeClass 
{ 
/// <summary> 
/// Store for the name property</summary> 
private string myName= null; 
/// <summary> 
/// The class constructor. </summary> 
public SomeClass() 
{ 
// TODO: Add Constructor Logic here 
} 
/// <summary> 
/// Name property </summary> 
/// <value> 
/// A value tag is used to describe the property value</value> 
public string Name 
{ 
get 
{ 
return myName; 
} 
} 
/// <summary> 
/// Description for SomeMethod.</summary> 
/// <paramname="s"> Parameter description for s goes here</param> 
/// <seealsocref="String"> 
/// You can use the crefattribute on any tag to reference a type or member 
/// and the compiler will check that the reference exists. </seealso> 
public void SomeMethod(string s) { } 
<?xml version="1.0"?> 
<doc> 
<assembly> 
<name>xmlsample</name> 
</assembly> 
<members> 
<member name="T:SomeClass"> 
<summary> 
Class level summary documentation goes here. 
</summary> 
<remarks> 
Longer comments can be associated with a type or member 
through the remarks tag 
</remarks> 
</member> 
<member name="F:SomeClass.myName"> 
<summary> 
Store for the name property 
</summary> 
</member> 
<member name="M:SomeClass.#ctor"> 
<summary>The class constructor.</summary> 
</member> 
<member name="M:SomeClass.SomeMethod(System.String)"> 
<summary>Description for SomeMethod.</summary> 
<paramname="s"> Parameter description for s goes here</param> 
<seealsocref="T:System.String"> 
You can use the crefattribute on any tag to reference a type or member 
and the compiler will check that the reference exists. </seealso> 
</member> 
…. 
</doc>
XML Documentation 
•To build the XML Documentation sample 
–To generate the sample XML documentation, type the following at the command prompt: 
•cscXMLsample.cs/doc:XMLsample.xml 
–To see the generated XML, issue the following command: 
•type XMLsample.xml
Ad

More Related Content

What's hot (20)

Javascript 101
Javascript 101Javascript 101
Javascript 101
Shlomi Komemi
 
Learn javascript easy steps
Learn javascript easy stepsLearn javascript easy steps
Learn javascript easy steps
prince Loffar
 
An Introduction to JavaScript
An Introduction to JavaScriptAn Introduction to JavaScript
An Introduction to JavaScript
tonyh1
 
Javascript Best Practices
Javascript Best PracticesJavascript Best Practices
Javascript Best Practices
Christian Heilmann
 
Java script basics
Java script basicsJava script basics
Java script basics
Shrivardhan Limbkar
 
Intro To Hibernate
Intro To HibernateIntro To Hibernate
Intro To Hibernate
Amit Himani
 
Patterns for JVM languages - Geecon 2014
Patterns for JVM languages - Geecon 2014Patterns for JVM languages - Geecon 2014
Patterns for JVM languages - Geecon 2014
Jaroslaw Palka
 
Java script -23jan2015
Java script -23jan2015Java script -23jan2015
Java script -23jan2015
Sasidhar Kothuru
 
Secrets of JavaScript Libraries
Secrets of JavaScript LibrariesSecrets of JavaScript Libraries
Secrets of JavaScript Libraries
jeresig
 
Java
Java Java
Java
Aashish Jain
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
Week3
Week3Week3
Week3
Will Gaybrick
 
Lab#1 - Front End Development
Lab#1 - Front End DevelopmentLab#1 - Front End Development
Lab#1 - Front End Development
Walid Ashraf
 
Java script
Java scriptJava script
Java script
Shyam Khant
 
Ado.net session11
Ado.net session11Ado.net session11
Ado.net session11
Niit Care
 
Hibernate
Hibernate Hibernate
Hibernate
Sunil OS
 
WEB TECHNOLOGIES JavaScript
WEB TECHNOLOGIES JavaScriptWEB TECHNOLOGIES JavaScript
WEB TECHNOLOGIES JavaScript
Jyothishmathi Institute of Technology and Science Karimnagar
 
Scala Reflection & Runtime MetaProgramming
Scala Reflection & Runtime MetaProgrammingScala Reflection & Runtime MetaProgramming
Scala Reflection & Runtime MetaProgramming
Meir Maor
 
Java script
 Java script Java script
Java script
bosybosy
 
Ajax
AjaxAjax
Ajax
Manav Prasad
 

Viewers also liked (20)

Procesamiento de XML en C#
Procesamiento de XML en C#Procesamiento de XML en C#
Procesamiento de XML en C#
Jordan-P
 
XML en .NET
XML en .NETXML en .NET
XML en .NET
brobelo
 
Asynchronous programming in .net 4.5 with c#
Asynchronous programming in .net 4.5 with c#Asynchronous programming in .net 4.5 with c#
Asynchronous programming in .net 4.5 with c#
Binu Bhasuran
 
.Net 3.5
.Net 3.5.Net 3.5
.Net 3.5
Pradeep Pajarla
 
Advanced c#
Advanced c#Advanced c#
Advanced c#
saranuru
 
27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examples27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examples
Quang Suma
 
.NET 4.0 Code Contracts (2010)
.NET 4.0 Code Contracts (2010).NET 4.0 Code Contracts (2010)
.NET 4.0 Code Contracts (2010)
Koen Metsu
 
Microsoft Managed Extensibility Framework
Microsoft Managed Extensibility FrameworkMicrosoft Managed Extensibility Framework
Microsoft Managed Extensibility Framework
Binu Bhasuran
 
Advanced C#. Part 2
Advanced C#. Part 2Advanced C#. Part 2
Advanced C#. Part 2
eleksdev
 
Advanced C#. Part 1
Advanced C#. Part 1Advanced C#. Part 1
Advanced C#. Part 1
eleksdev
 
.Net 4.0 Threading and Parallel Programming
.Net 4.0 Threading and Parallel Programming.Net 4.0 Threading and Parallel Programming
.Net 4.0 Threading and Parallel Programming
Alex Moore
 
Cloud Computing
Cloud ComputingCloud Computing
Cloud Computing
Biswajit Pratihari
 
What’s new in Visual Studio 2012 & .NET 4.5
What’s new in Visual Studio 2012 & .NET 4.5What’s new in Visual Studio 2012 & .NET 4.5
What’s new in Visual Studio 2012 & .NET 4.5
Robert MacLean
 
Patterns For Cloud Computing
Patterns For Cloud ComputingPatterns For Cloud Computing
Patterns For Cloud Computing
Simon Guest
 
Cloud Computing
Cloud ComputingCloud Computing
Cloud Computing
Biswajit Pratihari
 
SOA Unit I
SOA Unit ISOA Unit I
SOA Unit I
Dileep Kumar G
 
Web Application Development Fundamentals
Web Application Development FundamentalsWeb Application Development Fundamentals
Web Application Development Fundamentals
Mohammed Makhlouf
 
Introduction to xml
Introduction to xmlIntroduction to xml
Introduction to xml
Gtu Booker
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
Jussi Pohjolainen
 
Design Patterns (Examples in .NET)
Design Patterns (Examples in .NET)Design Patterns (Examples in .NET)
Design Patterns (Examples in .NET)
Aniruddha Chakrabarti
 
Procesamiento de XML en C#
Procesamiento de XML en C#Procesamiento de XML en C#
Procesamiento de XML en C#
Jordan-P
 
XML en .NET
XML en .NETXML en .NET
XML en .NET
brobelo
 
Asynchronous programming in .net 4.5 with c#
Asynchronous programming in .net 4.5 with c#Asynchronous programming in .net 4.5 with c#
Asynchronous programming in .net 4.5 with c#
Binu Bhasuran
 
Advanced c#
Advanced c#Advanced c#
Advanced c#
saranuru
 
27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examples27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examples
Quang Suma
 
.NET 4.0 Code Contracts (2010)
.NET 4.0 Code Contracts (2010).NET 4.0 Code Contracts (2010)
.NET 4.0 Code Contracts (2010)
Koen Metsu
 
Microsoft Managed Extensibility Framework
Microsoft Managed Extensibility FrameworkMicrosoft Managed Extensibility Framework
Microsoft Managed Extensibility Framework
Binu Bhasuran
 
Advanced C#. Part 2
Advanced C#. Part 2Advanced C#. Part 2
Advanced C#. Part 2
eleksdev
 
Advanced C#. Part 1
Advanced C#. Part 1Advanced C#. Part 1
Advanced C#. Part 1
eleksdev
 
.Net 4.0 Threading and Parallel Programming
.Net 4.0 Threading and Parallel Programming.Net 4.0 Threading and Parallel Programming
.Net 4.0 Threading and Parallel Programming
Alex Moore
 
What’s new in Visual Studio 2012 & .NET 4.5
What’s new in Visual Studio 2012 & .NET 4.5What’s new in Visual Studio 2012 & .NET 4.5
What’s new in Visual Studio 2012 & .NET 4.5
Robert MacLean
 
Patterns For Cloud Computing
Patterns For Cloud ComputingPatterns For Cloud Computing
Patterns For Cloud Computing
Simon Guest
 
Web Application Development Fundamentals
Web Application Development FundamentalsWeb Application Development Fundamentals
Web Application Development Fundamentals
Mohammed Makhlouf
 
Introduction to xml
Introduction to xmlIntroduction to xml
Introduction to xml
Gtu Booker
 
Ad

Similar to C# Advanced L03-XML+LINQ to XML (20)

Dom
Dom Dom
Dom
Surinder Kaur
 
java API for XML DOM
java API for XML DOMjava API for XML DOM
java API for XML DOM
Surinder Kaur
 
Understanding XML DOM
Understanding XML DOMUnderstanding XML DOM
Understanding XML DOM
Om Vikram Thapa
 
Simple xml in .net
Simple xml in .netSimple xml in .net
Simple xml in .net
Vi Vo Hung
 
Dom
DomDom
Dom
Chandan Kumar
 
Part 7
Part 7Part 7
Part 7
NOHA AW
 
The xml
The xmlThe xml
The xml
Raghu nath
 
XML stands for EXtensible Markup Language
XML stands for EXtensible Markup LanguageXML stands for EXtensible Markup Language
XML stands for EXtensible Markup Language
NetajiGandi1
 
Internet and Web Technology (CLASS-7) [XML and AJAX] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-7) [XML and AJAX] | NIC/NIELIT Web TechnologyInternet and Web Technology (CLASS-7) [XML and AJAX] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-7) [XML and AJAX] | NIC/NIELIT Web Technology
Ayes Chinmay
 
Jdom how it works & how it opened the java process
Jdom how it works & how it opened the java processJdom how it works & how it opened the java process
Jdom how it works & how it opened the java process
Hicham QAISSI
 
Advanced Web Programming Chapter 12
Advanced Web Programming Chapter 12Advanced Web Programming Chapter 12
Advanced Web Programming Chapter 12
RohanMistry15
 
Xsd restrictions, xsl elements, dhtml
Xsd restrictions, xsl elements, dhtmlXsd restrictions, xsl elements, dhtml
Xsd restrictions, xsl elements, dhtml
AMIT VIRAMGAMI
 
Xml And JSON Java
Xml And JSON JavaXml And JSON Java
Xml And JSON Java
Henry Addo
 
XML Document Object Model (DOM)
XML Document Object Model (DOM)XML Document Object Model (DOM)
XML Document Object Model (DOM)
BOSS Webtech
 
XML
XMLXML
XML
baabtra.com - No. 1 supplier of quality freshers
 
Jquery fundamentals
Jquery fundamentalsJquery fundamentals
Jquery fundamentals
Salvatore Fazio
 
Xml writers
Xml writersXml writers
Xml writers
Raghu nath
 
Processing XML and Spreadsheet data in Go
Processing XML and Spreadsheet data in GoProcessing XML and Spreadsheet data in Go
Processing XML and Spreadsheet data in Go
Ri Xu
 
Xml
XmlXml
Xml
sudhakar mandal
 
Xml 2
Xml  2 Xml  2
Xml 2
pavishkumarsingh
 
java API for XML DOM
java API for XML DOMjava API for XML DOM
java API for XML DOM
Surinder Kaur
 
Simple xml in .net
Simple xml in .netSimple xml in .net
Simple xml in .net
Vi Vo Hung
 
XML stands for EXtensible Markup Language
XML stands for EXtensible Markup LanguageXML stands for EXtensible Markup Language
XML stands for EXtensible Markup Language
NetajiGandi1
 
Internet and Web Technology (CLASS-7) [XML and AJAX] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-7) [XML and AJAX] | NIC/NIELIT Web TechnologyInternet and Web Technology (CLASS-7) [XML and AJAX] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-7) [XML and AJAX] | NIC/NIELIT Web Technology
Ayes Chinmay
 
Jdom how it works & how it opened the java process
Jdom how it works & how it opened the java processJdom how it works & how it opened the java process
Jdom how it works & how it opened the java process
Hicham QAISSI
 
Advanced Web Programming Chapter 12
Advanced Web Programming Chapter 12Advanced Web Programming Chapter 12
Advanced Web Programming Chapter 12
RohanMistry15
 
Xsd restrictions, xsl elements, dhtml
Xsd restrictions, xsl elements, dhtmlXsd restrictions, xsl elements, dhtml
Xsd restrictions, xsl elements, dhtml
AMIT VIRAMGAMI
 
Xml And JSON Java
Xml And JSON JavaXml And JSON Java
Xml And JSON Java
Henry Addo
 
XML Document Object Model (DOM)
XML Document Object Model (DOM)XML Document Object Model (DOM)
XML Document Object Model (DOM)
BOSS Webtech
 
Processing XML and Spreadsheet data in Go
Processing XML and Spreadsheet data in GoProcessing XML and Spreadsheet data in Go
Processing XML and Spreadsheet data in Go
Ri Xu
 
Ad

More from Mohammad Shaker (20)

12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate
Mohammad Shaker
 
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Mohammad Shaker
 
Interaction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyInteraction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with Psychology
Mohammad Shaker
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015
Mohammad Shaker
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game Development
Mohammad Shaker
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and Wearables
Mohammad Shaker
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - Color
Mohammad Shaker
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - Typography
Mohammad Shaker
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and Coupling
Mohammad Shaker
 
Android L05 - Storage
Android L05 - StorageAndroid L05 - Storage
Android L05 - Storage
Mohammad Shaker
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and Threading
Mohammad Shaker
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOS
Mohammad Shaker
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile Constraints
Mohammad Shaker
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and Grids
Mohammad Shaker
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and Gaming
Mohammad Shaker
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / Parse
Mohammad Shaker
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and Utilities
Mohammad Shaker
 
Android L03 - Styles and Themes
Android L03 - Styles and Themes Android L03 - Styles and Themes
Android L03 - Styles and Themes
Mohammad Shaker
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and Adapters
Mohammad Shaker
 
Android L01 - Warm Up
Android L01 - Warm UpAndroid L01 - Warm Up
Android L01 - Warm Up
Mohammad Shaker
 
12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate
Mohammad Shaker
 
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Mohammad Shaker
 
Interaction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyInteraction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with Psychology
Mohammad Shaker
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015
Mohammad Shaker
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game Development
Mohammad Shaker
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and Wearables
Mohammad Shaker
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - Color
Mohammad Shaker
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - Typography
Mohammad Shaker
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and Coupling
Mohammad Shaker
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and Threading
Mohammad Shaker
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOS
Mohammad Shaker
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile Constraints
Mohammad Shaker
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and Grids
Mohammad Shaker
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and Gaming
Mohammad Shaker
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / Parse
Mohammad Shaker
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and Utilities
Mohammad Shaker
 
Android L03 - Styles and Themes
Android L03 - Styles and Themes Android L03 - Styles and Themes
Android L03 - Styles and Themes
Mohammad Shaker
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and Adapters
Mohammad Shaker
 

Recently uploaded (20)

Medical Device Cybersecurity Threat & Risk Scoring
Medical Device Cybersecurity Threat & Risk ScoringMedical Device Cybersecurity Threat & Risk Scoring
Medical Device Cybersecurity Threat & Risk Scoring
ICS
 
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
Ranking Google
 
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.pptPassive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
IES VE
 
Do not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your causeDo not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your cause
Fexle Services Pvt. Ltd.
 
Adobe Audition Crack FRESH Version 2025 FREE
Adobe Audition Crack FRESH Version 2025 FREEAdobe Audition Crack FRESH Version 2025 FREE
Adobe Audition Crack FRESH Version 2025 FREE
zafranwaqar90
 
A Comprehensive Guide to CRM Software Benefits for Every Business Stage
A Comprehensive Guide to CRM Software Benefits for Every Business StageA Comprehensive Guide to CRM Software Benefits for Every Business Stage
A Comprehensive Guide to CRM Software Benefits for Every Business Stage
SynapseIndia
 
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
OnePlan Solutions
 
Artificial hand using embedded system.pptx
Artificial hand using embedded system.pptxArtificial hand using embedded system.pptx
Artificial hand using embedded system.pptx
bhoomigowda12345
 
Best HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRMBest HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRM
accordHRM
 
GC Tuning: A Masterpiece in Performance Engineering
GC Tuning: A Masterpiece in Performance EngineeringGC Tuning: A Masterpiece in Performance Engineering
GC Tuning: A Masterpiece in Performance Engineering
Tier1 app
 
Download 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-ActivatedDownload 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-Activated
Web Designer
 
AEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural MeetingAEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural Meeting
jennaf3
 
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by AjathMobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Ajath Infotech Technologies LLC
 
wAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptxwAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptx
SimonedeGijt
 
How I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetryHow I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetry
Cees Bos
 
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb ClarkDeploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Peter Caitens
 
Autodesk Inventor Crack (2025) Latest
Autodesk Inventor    Crack (2025) LatestAutodesk Inventor    Crack (2025) Latest
Autodesk Inventor Crack (2025) Latest
Google
 
Memory Management and Leaks in Postgres from pgext.day 2025
Memory Management and Leaks in Postgres from pgext.day 2025Memory Management and Leaks in Postgres from pgext.day 2025
Memory Management and Leaks in Postgres from pgext.day 2025
Phil Eaton
 
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdfTop Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
evrigsolution
 
The Elixir Developer - All Things Open
The Elixir Developer - All Things OpenThe Elixir Developer - All Things Open
The Elixir Developer - All Things Open
Carlo Gilmar Padilla Santana
 
Medical Device Cybersecurity Threat & Risk Scoring
Medical Device Cybersecurity Threat & Risk ScoringMedical Device Cybersecurity Threat & Risk Scoring
Medical Device Cybersecurity Threat & Risk Scoring
ICS
 
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
Ranking Google
 
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.pptPassive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
IES VE
 
Do not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your causeDo not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your cause
Fexle Services Pvt. Ltd.
 
Adobe Audition Crack FRESH Version 2025 FREE
Adobe Audition Crack FRESH Version 2025 FREEAdobe Audition Crack FRESH Version 2025 FREE
Adobe Audition Crack FRESH Version 2025 FREE
zafranwaqar90
 
A Comprehensive Guide to CRM Software Benefits for Every Business Stage
A Comprehensive Guide to CRM Software Benefits for Every Business StageA Comprehensive Guide to CRM Software Benefits for Every Business Stage
A Comprehensive Guide to CRM Software Benefits for Every Business Stage
SynapseIndia
 
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
OnePlan Solutions
 
Artificial hand using embedded system.pptx
Artificial hand using embedded system.pptxArtificial hand using embedded system.pptx
Artificial hand using embedded system.pptx
bhoomigowda12345
 
Best HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRMBest HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRM
accordHRM
 
GC Tuning: A Masterpiece in Performance Engineering
GC Tuning: A Masterpiece in Performance EngineeringGC Tuning: A Masterpiece in Performance Engineering
GC Tuning: A Masterpiece in Performance Engineering
Tier1 app
 
Download 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-ActivatedDownload 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-Activated
Web Designer
 
AEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural MeetingAEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural Meeting
jennaf3
 
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by AjathMobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Ajath Infotech Technologies LLC
 
wAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptxwAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptx
SimonedeGijt
 
How I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetryHow I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetry
Cees Bos
 
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb ClarkDeploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Peter Caitens
 
Autodesk Inventor Crack (2025) Latest
Autodesk Inventor    Crack (2025) LatestAutodesk Inventor    Crack (2025) Latest
Autodesk Inventor Crack (2025) Latest
Google
 
Memory Management and Leaks in Postgres from pgext.day 2025
Memory Management and Leaks in Postgres from pgext.day 2025Memory Management and Leaks in Postgres from pgext.day 2025
Memory Management and Leaks in Postgres from pgext.day 2025
Phil Eaton
 
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdfTop Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
evrigsolution
 

C# Advanced L03-XML+LINQ to XML

  • 1. Mohammad Shaker mohammadshaker.com @ZGTRShaker 2011, 2012, 2013, 2014 C# Advanced L03-XML & LINQ TO XML
  • 2. XML
  • 4. XML •HTML and XHTML? •XML –Extensible Markup Language –A metalanguagethat allows users to define their own customized markup languages, especially in order to display documents on the World Wide Web (WWW). •XSD –XML Schema Definition language •XDR –XML -Data Reduced schemas. •Tags
  • 5. XML <contacts> <contactcontactId="2"> <firstName>Mohammad</firstName> <lastName>Shaker</lastName> </contact> <contactcontactId="3"> <firstName>Hamza</firstName> <lastName>Smith</lastName> </contact> </contacts>
  • 6. XML <contacts> <contactcontactId="2"> <firstName>Mohammad</firstName> <lastName>Shaker</lastName> </contact> <contactcontactId="3"> <firstName>Hamza</firstName> <lastName>Smith</lastName> </contact> </contacts>
  • 7. XML <contacts> <contactcontactId="2"> <firstName>Mohammad</firstName> <lastName>Shaker</lastName> </contact> <contactcontactId="3"> <firstName>Hamza</firstName> <lastName>Smith</lastName> </contact> </contacts> Contacts
  • 8. XML <contacts> <contactcontactId="2"> <firstName>Mohammad</firstName> <lastName>Shaker</lastName> </contact> <contactcontactId="3"> <firstName>Hamza</firstName> <lastName>Smith</lastName> </contact> </contacts> Contacts
  • 9. XML <contacts> <contactcontactId="2"> <firstName>Mohammad</firstName> <lastName>Shaker</lastName> </contact> <contactcontactId="3"> <firstName>Hamza</firstName> <lastName>Smith</lastName> </contact> </contacts> Contacts Contact Contact
  • 10. XML <contacts> <contactcontactId="2"> <firstName>Mohammad</firstName> <lastName>Shaker</lastName> </contact> <contactcontactId="3"> <firstName>Hamza</firstName> <lastName>Smith</lastName> </contact> </contacts> Contacts Mohammad Shaker Hamza Smith
  • 11. XML .NET Classes Class Description XmlNode Represents a single node in a document tree. It is the base of many of the classes shown in this chapter. If this node represents the root of an XML document, you can navigate to any position in the document from it. XmlDocument Extends the XmlNodeclass, but is often the first object you use when using XML. That’s because this class is used to load and save data from disk or elsewhere.
  • 12. XML .NET Classes Class Description XmlElement Represents a single element in the XML document. XmlElementis derived from XmlLinkedNode, which in turn is derived from XmlNode. XmlAttribute Represents a single attribute. Like the XmlDocumentclass, it is derived from the XmlNodeclass. XmlText Represents the text between a starting tag and a closing tag. XmlComment Represents a special kind of node that is not regarded as part of the document other than to provide information to the reader about parts of the document. XmlNodeList Represents a collection of nodes.
  • 13. Creating an XML Node in an XML FileProgrammatically
  • 14. Creating XML Node static void Main(string[] args) { XmlDocumentxmlDoc= new XmlDocument(); XmlNoderootNode= xmlDoc.CreateElement("users"); xmlDoc.AppendChild(rootNode); XmlNodeuserNode= xmlDoc.CreateElement("user"); XmlAttributeattribute = xmlDoc.CreateAttribute("age"); attribute.Value= "42"; userNode.Attributes.Append(attribute); userNode.InnerText= "John Doe"; rootNode.AppendChild(userNode); userNode= xmlDoc.CreateElement("user"); attribute = xmlDoc.CreateAttribute("age"); attribute.Value= "39"; userNode.Attributes.Append(attribute); userNode.InnerText= "Jane Doe"; rootNode.AppendChild(userNode); xmlDoc.Save("test-doc.xml"); } Create an XML File <users> <user age="42">John Doe</user> <user age="39">Jane Doe</user> </users>
  • 15. Creating XML Node static void Main(string[] args) { XmlDocumentxmlDoc= new XmlDocument(); XmlNoderootNode= xmlDoc.CreateElement("users"); xmlDoc.AppendChild(rootNode); XmlNodeuserNode= xmlDoc.CreateElement("user"); XmlAttributeattribute = xmlDoc.CreateAttribute("age"); attribute.Value= "42"; userNode.Attributes.Append(attribute); userNode.InnerText= "John Doe"; rootNode.AppendChild(userNode); userNode= xmlDoc.CreateElement("user"); attribute = xmlDoc.CreateAttribute("age"); attribute.Value= "39"; userNode.Attributes.Append(attribute); userNode.InnerText= "Jane Doe"; rootNode.AppendChild(userNode); xmlDoc.Save("test-doc.xml"); } Create an XML Node users <users> <user age="42">John Doe</user> <user age="39">Jane Doe</user> </users>
  • 16. Creating XML Node static void Main(string[] args) { XmlDocumentxmlDoc= new XmlDocument(); XmlNoderootNode= xmlDoc.CreateElement("users"); xmlDoc.AppendChild(rootNode); XmlNodeuserNode= xmlDoc.CreateElement("user"); XmlAttributeattribute = xmlDoc.CreateAttribute("age"); attribute.Value= "42"; userNode.Attributes.Append(attribute); userNode.InnerText= "John Doe"; rootNode.AppendChild(userNode); userNode= xmlDoc.CreateElement("user"); attribute = xmlDoc.CreateAttribute("age"); attribute.Value= "39"; userNode.Attributes.Append(attribute); userNode.InnerText= "Jane Doe"; rootNode.AppendChild(userNode); xmlDoc.Save("test-doc.xml"); } Append the XML Node to the XML File <users> <user age="42">John Doe</user> <user age="39">Jane Doe</user> </users>
  • 17. Creating XML Node static void Main(string[] args) { XmlDocumentxmlDoc= new XmlDocument(); XmlNoderootNode= xmlDoc.CreateElement("users"); xmlDoc.AppendChild(rootNode); XmlNodeuserNode= xmlDoc.CreateElement("user"); XmlAttributeattribute = xmlDoc.CreateAttribute("age"); attribute.Value= "42"; userNode.Attributes.Append(attribute); userNode.InnerText= "John Doe"; rootNode.AppendChild(userNode); userNode= xmlDoc.CreateElement("user"); attribute = xmlDoc.CreateAttribute("age"); attribute.Value= "39"; userNode.Attributes.Append(attribute); userNode.InnerText= "Jane Doe"; rootNode.AppendChild(userNode); xmlDoc.Save("test-doc.xml"); } Create an XML Node user <users> <user age="42">John Doe</user> <user age="39">Jane Doe</user> </users>
  • 18. Creating XML Node static void Main(string[] args) { XmlDocumentxmlDoc= new XmlDocument(); XmlNoderootNode= xmlDoc.CreateElement("users"); xmlDoc.AppendChild(rootNode); XmlNodeuserNode= xmlDoc.CreateElement("user"); XmlAttributeattribute = xmlDoc.CreateAttribute("age"); attribute.Value= "42"; userNode.Attributes.Append(attribute); userNode.InnerText= "John Doe"; rootNode.AppendChild(userNode); userNode= xmlDoc.CreateElement("user"); attribute = xmlDoc.CreateAttribute("age"); attribute.Value= "39"; userNode.Attributes.Append(attribute); userNode.InnerText= "Jane Doe"; rootNode.AppendChild(userNode); xmlDoc.Save("test-doc.xml"); } Append the node: user to the root node: users <users> <user age="42">John Doe</user> <user age="39">Jane Doe</user> </users>
  • 19. Creating XML Node static void Main(string[] args) { XmlDocumentxmlDoc= new XmlDocument(); XmlNoderootNode= xmlDoc.CreateElement("users"); xmlDoc.AppendChild(rootNode); XmlNodeuserNode= xmlDoc.CreateElement("user"); XmlAttributeattribute = xmlDoc.CreateAttribute("age"); attribute.Value= "42"; userNode.Attributes.Append(attribute); userNode.InnerText= "John Doe"; rootNode.AppendChild(userNode); userNode= xmlDoc.CreateElement("user"); attribute = xmlDoc.CreateAttribute("age"); attribute.Value= "39"; userNode.Attributes.Append(attribute); userNode.InnerText= "Jane Doe"; rootNode.AppendChild(userNode); xmlDoc.Save("test-doc.xml"); } Create another XML Node user <users> <user age="42">John Doe</user> <user age="39">Jane Doe</user> </users>
  • 20. Creating XML Node static void Main(string[] args) { XmlDocumentxmlDoc= new XmlDocument(); XmlNoderootNode= xmlDoc.CreateElement("users"); xmlDoc.AppendChild(rootNode); XmlNodeuserNode= xmlDoc.CreateElement("user"); XmlAttributeattribute = xmlDoc.CreateAttribute("age"); attribute.Value= "42"; userNode.Attributes.Append(attribute); userNode.InnerText= "John Doe"; rootNode.AppendChild(userNode); userNode= xmlDoc.CreateElement("user"); attribute = xmlDoc.CreateAttribute("age"); attribute.Value= "39"; userNode.Attributes.Append(attribute); userNode.InnerText= "Jane Doe"; rootNode.AppendChild(userNode); xmlDoc.Save("test-doc.xml"); } Save the file where you want <users> <user age="42">John Doe</user> <user age="39">Jane Doe</user> </users>
  • 21. Building a Node -A Faster Way? XElementxml=newXElement("contacts", newXElement("contact", newXAttribute("contactId", "2"), newXElement("firstName", "Mohammad"), newXElement("lastName", "Shaker") ), newXElement("contact", newXAttribute("contactId", "3"), newXElement("firstName", "Hamza"), newXElement("lastName", "Smith") ) ); Console.WriteLine(xml);
  • 22. Building a Node -A Faster Way? XElementxml=newXElement("contacts", newXElement("contact", newXAttribute("contactId", "2"), newXElement("firstName", "Mohammad"), newXElement("lastName", "Shaker") ), newXElement("contact", newXAttribute("contactId", "3"), newXElement("firstName", "Hamza"), newXElement("lastName", "Smith") ) ); Console.WriteLine(xml); <contacts> <contactcontactId="2"> <firstName>Mohammad</firstName> <lastName>Shaker</lastName> </contact> <contactcontactId="3"> <firstName>Hamza</firstName> <lastName>Smith</lastName> </contact> </contacts>
  • 28. XML <?xml version="1.0" encoding="utf-8"?> <RoomsCoordProperties> <LocationDimension> 10 </LocationDimension> <Number> 5 </Number> <Width> 12 </Width> <Length> 12 </Length> <Height> 3 </Height> <Opacity> 0.3 </Opacity> <BasicLocationLength> 4 </BasicLocationLength> <Room id="3"> <LocationID> 0 </LocationID> <Width> 12 </Width> <Length> 12 </Length> <Height> 3 </Height> <Opacity> 0.3 </Opacity> </Room> … public static void InitializeRoomFromXMLFile(Room myRoom, introomNumber) { XmlDocumentxmlDoc= new XmlDocument(); xmlDoc.Load(XMLFilePath); XmlNodeListxmlNodeList= xmlDoc.GetElementsByTagName("Room"); myRoom.RoomID= Int32.Parse(xmlNodeList[roomNumber].Attributes[0].FirstChild.Value); myRoom.RoomLocationID= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[0].FirstChild.Value); myRoom.RoomWidth= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[1].FirstChild.Value); myRoom.RoomLength= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[2].FirstChild.Value); myRoom.RoomHeight= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[3].FirstChild.Value); myRoom.RoomOpacity= Double.Parse(xmlNodeList[roomNumber].ChildNodes[4].FirstChild.Value); }
  • 29. XML <?xml version="1.0" encoding="utf-8"?> <RoomsCoordProperties> <LocationDimension> 10 </LocationDimension> <Number> 5 </Number> <Width> 12 </Width> <Length> 12 </Length> <Height> 3 </Height> <Opacity> 0.3 </Opacity> <BasicLocationLength> 4 </BasicLocationLength> <Room id="3"> <LocationID> 0 </LocationID> <Width> 12 </Width> <Length> 12 </Length> <Height> 3 </Height> <Opacity> 0.3 </Opacity> </Room> … public static void InitializeRoomFromXMLFile(Room myRoom, introomNumber) { XmlDocumentxmlDoc= new XmlDocument(); xmlDoc.Load(XMLFilePath); XmlNodeListxmlNodeList= xmlDoc.GetElementsByTagName("Room"); myRoom.RoomID= Int32.Parse(xmlNodeList[roomNumber].Attributes[0].FirstChild.Value); myRoom.RoomLocationID= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[0].FirstChild.Value); myRoom.RoomWidth= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[1].FirstChild.Value); myRoom.RoomLength= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[2].FirstChild.Value); myRoom.RoomHeight= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[3].FirstChild.Value); myRoom.RoomOpacity= Double.Parse(xmlNodeList[roomNumber].ChildNodes[4].FirstChild.Value); }
  • 30. XML <?xml version="1.0" encoding="utf-8"?> <RoomsCoordProperties> <LocationDimension> 10 </LocationDimension> <Number> 5 </Number> <Width> 12 </Width> <Length> 12 </Length> <Height> 3 </Height> <Opacity> 0.3 </Opacity> <BasicLocationLength> 4 </BasicLocationLength> <Room id="3"> <LocationID> 0 </LocationID> <Width> 12 </Width> <Length> 12 </Length> <Height> 3 </Height> <Opacity> 0.3 </Opacity> </Room> … public static void InitializeRoomFromXMLFile(Room myRoom, introomNumber) { XmlDocumentxmlDoc= new XmlDocument(); xmlDoc.Load(XMLFilePath); XmlNodeListxmlNodeList= xmlDoc.GetElementsByTagName("Room"); myRoom.RoomID= Int32.Parse(xmlNodeList[roomNumber].Attributes[0].FirstChild.Value); myRoom.RoomLocationID= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[0].FirstChild.Value); myRoom.RoomWidth= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[1].FirstChild.Value); myRoom.RoomLength= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[2].FirstChild.Value); myRoom.RoomHeight= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[3].FirstChild.Value); myRoom.RoomOpacity= Double.Parse(xmlNodeList[roomNumber].ChildNodes[4].FirstChild.Value); }
  • 31. XML <?xml version="1.0" encoding="utf-8"?> <RoomsCoordProperties> <LocationDimension> 10 </LocationDimension> <Number> 5 </Number> <Width> 12 </Width> <Length> 12 </Length> <Height> 3 </Height> <Opacity> 0.3 </Opacity> <BasicLocationLength> 4 </BasicLocationLength> <Room id="3"> <LocationID> 0 </LocationID> <Width> 12 </Width> <Length> 12 </Length> <Height> 3 </Height> <Opacity> 0.3 </Opacity> </Room> … public static void InitializeRoomFromXMLFile(Room myRoom, introomNumber) { XmlDocumentxmlDoc= new XmlDocument(); xmlDoc.Load(XMLFilePath); XmlNodeListxmlNodeList= xmlDoc.GetElementsByTagName("Room"); myRoom.RoomID= Int32.Parse(xmlNodeList[roomNumber].Attributes[0].FirstChild.Value); myRoom.RoomLocationID= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[0].FirstChild.Value); myRoom.RoomWidth= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[1].FirstChild.Value); myRoom.RoomLength= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[2].FirstChild.Value); myRoom.RoomHeight= Int32.Parse(xmlNodeList[roomNumber].ChildNodes[3].FirstChild.Value); myRoom.RoomOpacity= Double.Parse(xmlNodeList[roomNumber].ChildNodes[4].FirstChild.Value); }
  • 32. LINQ to XMLXML to LINQ
  • 33. LINQ and XML •LINQ to XML •XML to LINQ varquery = from p in people where p.CanCode select new Xelement(“Person”, new Xattribute(“Age”, p.Age), p.Name) varx = new XElement(“People”, from p in people where p.CanCode select new Xelement(“Person”, new Xattribute(“Age”, p.Age), p.Name) )
  • 34. LINQ to XML •Let’s have the following XML file <customers> <customerid="84"> <namevalue="Sample Name"/> </customer> <customerid="89"> <namevalue="Sample Name 2"/> </customer> <customerid="80"> <namevalue="Sample Name 3"/> </customer> </customers>
  • 35. LINQ to XML XmlNodesearched=null; XmlDocumentdoc=newXmlDocument(); doc.Load(@"D:Temporarycustomers.xml"); foreach(XmlNodenodeindoc.SelectNodes("/customers/customer")) { if(node.Attributes["id"].InnerText=="80") { searched=node; break; } }
  • 36. LINQ to XML XmlNodesearched=null; XmlDocumentdoc=newXmlDocument(); doc.Load(@"D:Temporarycustomers.xml"); foreach(XmlNodenodeindoc.SelectNodes("/customers/customer")) { if(node.Attributes["id"].InnerText=="80") { searched=node; break; } } XElementmain=XElement.Load(@"D:Temporarycustomers.xml"); IEnumerable<XElement>searched= fromcinmain.Elements("customer") where(string)c.Attribute("id") =="80" selectc;
  • 37. LINQ to XML XmlNodesearched=null; XmlDocumentdoc=newXmlDocument(); doc.Load(@"D:Temporarycustomers.xml"); foreach(XmlNodenodeindoc.SelectNodes("/customers/customer")) { if(node.Attributes["id"].InnerText=="80") { searched=node; break; } } XElementmain=XElement.Load(@"D:Temporarycustomers.xml"); IEnumerable<XElement>searched= fromcinmain.Elements("customer") where(string)c.Attribute("id") =="80" selectc; <customers> <customerid="84"> <namevalue="Sample Name"/> </customer> <customerid="89"> <namevalue="Sample Name 2"/> </customer> <customerid="80"> <namevalue="Sample Name 3"/> </customer> </customers>
  • 38. LINQ to XML XmlNodesearched=null; XmlDocumentdoc=newXmlDocument(); doc.Load(@"D:Temporarycustomers.xml"); foreach(XmlNodenodeindoc.SelectNodes("/customers/customer")) { if(node.Attributes["id"].InnerText=="80") { searched=node; break; } } XElementmain=XElement.Load(@"D:Temporarycustomers.xml"); IEnumerable<XElement>searched= fromcinmain.Elements("customer") where(string)c.Attribute("id") =="80" selectc; <customerid="80"> <namevalue="Sample Name 3"/> </customer> <customers> <customerid="84"> <namevalue="Sample Name"/> </customer> <customerid="89"> <namevalue="Sample Name 2"/> </customer> <customerid="80"> <namevalue="Sample Name 3"/> </customer> </customers>
  • 39. LINQ to XML XElementmain=XElement.Load(@"D:Temporarycustomers.xml"); IEnumerable<XElement>searched= fromcinmain.Elements("customer") where(string)c.Element("name").Attribute("value") =="Sample Name" selectc; <customers> <customerid="84"> <namevalue="Sample Name"/> </customer> <customerid="89"> <namevalue="Sample Name 2"/> </customer> <customerid="80"> <namevalue="Sample Name 3"/> </customer> </customers>
  • 40. LINQ to XML XElementmain=XElement.Load(@"D:Temporarycustomers.xml"); IEnumerable<XElement>searched= fromcinmain.Elements("customer") where(string)c.Element("name").Attribute("value") =="Sample Name" selectc; <customerid="84"> <namevalue="Sample Name"/> </customer> <customers> <customerid="84"> <namevalue="Sample Name"/> </customer> <customerid="89"> <namevalue="Sample Name 2"/> </customer> <customerid="80"> <namevalue="Sample Name 3"/> </customer> </customers>
  • 41. LINQ to XML XElementmain=XElement.Load(@"D:Temporarycustomers.xml"); IEnumerable<XElement>searched= fromcinmain.Elements("customer") where(string)c.Attribute("id") =="84" &&(string)c.Element("name").Attribute("value") =="Sample Name" selectc; <customers> <customerid="84"> <namevalue="Sample Name"/> </customer> <customerid="89"> <namevalue="Sample Name 2"/> </customer> <customerid="80"> <namevalue="Sample Name 3"/> </customer> </customers>
  • 42. LINQ to XML XElementmain=XElement.Load(@"D:Temporarycustomers.xml"); IEnumerable<XElement>searched= fromcinmain.Elements("customer") where(string)c.Attribute("id") =="84" &&(string)c.Element("name").Attribute("value") =="Sample Name" selectc; <customerid="84"> <namevalue="Sample Name"/> </customer> <customers> <customerid="84"> <namevalue="Sample Name"/> </customer> <customerid="89"> <namevalue="Sample Name 2"/> </customer> <customerid="80"> <namevalue="Sample Name 3"/> </customer> </customers>
  • 43. Performanceof LINQ •LINQ has more control and efficiency in O/R Mapping than NHibernate –LINQ: ExternlMapping or Attribute Mapping –NHibernate: ExternlMapping •Because of mapping, LINQ is lower than database tools such as SqlDataReaderor SqlDataAdapter –In large dataset, their performance are more and more similar
  • 45. XML SerializationNot Always an Easy, Straightforward Task
  • 47. XML SerializationSerialize a class that simply consists of public fields and properties into an XML file
  • 48. XML SerializationUse XML serialization to generate an XML stream that conforms to a specific XML Schema (XSD) document.
  • 50. XML Serialization namespace XMLTest1 { public class Test { public String value1; public String value2; } class Program { static void Main(string[] args) { Test myTest= new Test() { value1 = "Value 1", value2 = "Value 2" }; XmlSerializerx = new XmlSerializer(myTest.GetType()); x.Serialize(Console.Out, myTest); Console.ReadKey(); } } }
  • 51. XML Serialization namespace XMLTest1 { public class Test { public String value1; public String value2; } class Program { static void Main(string[] args) { Test myTest= new Test() { value1 = "Value 1", value2 = "Value 2" }; XmlSerializerx = new XmlSerializer(myTest.GetType()); x.Serialize(Console.Out, myTest); Console.ReadKey(); } } } <?xml version="1.0" encoding="ibm850"?> <Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <value1>Value 1</value1> <value2>Value 2</value2> </Test> That’s Cool!
  • 53. XML DeSerialization namespace XMLTest1 { public class Test { public String value1; public String value2; } class Program { static void Main(string[] args) { String xData= "<?xml version="1.0" encoding="ibm850"?><Test xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><value1>Value 1</value1><value2>Value 2</value2></Test>"; XmlSerializerx = new XmlSerializer(typeof(Test)); Test myTest= (Test)x.Deserialize(new StringReader(xData)); Console.WriteLine("V1: " + myTest.value1); Console.WriteLine("V2: " + myTest.value2); Console.ReadKey(); } } }
  • 54. XML DeSerialization namespace XMLTest1 { public class Test { public String value1; public String value2; } class Program { static void Main(string[] args) { String xData= "<?xml version="1.0" encoding="ibm850"?><Test xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><value1>Value 1</value1><value2>Value 2</value2></Test>"; XmlSerializerx = new XmlSerializer(typeof(Test)); Test myTest= (Test)x.Deserialize(new StringReader(xData)); Console.WriteLine("V1: " + myTest.value1); Console.WriteLine("V2: " + myTest.value2); Console.ReadKey(); } } }
  • 55. XML Serialization and Attributes
  • 56. XML Serialization and Attributes [XmlRoot("XTest")] public class Test { [XmlElement(ElementName="V1")] public String value1; [XmlElement(ElementName="V2")] public String value2; [XmlArray("OtherValues")] [XmlArrayItem("OValue")] public List others = new List(); }
  • 57. XML Serialization and Attributes [XmlRoot("XTest")] public class Test { [XmlElement(ElementName="V1")] public String value1; [XmlElement(ElementName="V2")] public String value2; [XmlArray("OtherValues")] [XmlArrayItem("OValue")] public List others = new List(); } <?xml version="1.0" encoding="ibm850"?> <XTestxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <V1>A1</V1> <V2>B1</V2> <OtherValues> <OValue>Test</OValue> </OtherValues> </XTest>
  • 58. XML Serialization and Attributes [XmlRoot("XTest")] public class Test { [XmlElement(ElementName="V1")] public String value1; [XmlElement(ElementName="V2")] public String value2; [XmlArray("OtherValues")] [XmlArrayItem("OValue")] public List others = new List(); } <?xml version="1.0" encoding="ibm850"?> <XTestxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <V1>A1</V1> <V2>B1</V2> <OtherValues> <OValue>Test</OValue> </OtherValues> </XTest>
  • 59. XML Serialization and Attributes [XmlRoot("XTest")] public class Test { [XmlElement(ElementName="V1")] public String value1; [XmlElement(ElementName="V2")] public String value2; [XmlArray("OtherValues")] [XmlArrayItem("OValue")] public List others = new List(); } <?xml version="1.0" encoding="ibm850"?> <XTestxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <V1>A1</V1> <V2>B1</V2> <OtherValues> <OValue>Test</OValue> </OtherValues> </XTest>
  • 60. XML Serialization and Attributes [XmlRoot("XTest")] public class Test { [XmlElement(ElementName="V1")] public String value1; [XmlElement(ElementName="V2")] public String value2; [XmlArray("OtherValues")] [XmlArrayItem("OValue")] public List others = new List(); } <?xml version="1.0" encoding="ibm850"?> <XTestxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <V1>A1</V1> <V2>B1</V2> <OtherValues> <OValue>Test</OValue> </OtherValues> </XTest>
  • 61. XML Serialization and Attributes [XmlRoot("XTest")] public class Test { [XmlElement(ElementName="V1")] public String value1; [XmlElement(ElementName="V2")] public String value2; [XmlArray("OtherValues")] [XmlArrayItem("OValue")] public List others = new List(); } <?xml version="1.0" encoding="ibm850"?> <XTestxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <V1>A1</V1> <V2>B1</V2> <OtherValues> <OValue>Test</OValue> </OtherValues> </XTest>
  • 63. XML DocumentationC# provides a mechanism for developers to document their code using XML. In source code files, lines that begin with ///and that precede a user-defined type such as a class, delegate, or interface; a member such as a field, event, property, or method; or a namespace declaration can be processed as comments and placed in a file.
  • 64. XML Documentation /// <summary> /// Class level summary documentation goes here.</summary> /// <remarks> /// Longer comments can be associated with a type or member /// through the remarks tag</remarks> public class SomeClass { /// <summary> /// Store for the name property</summary> private string myName= null; /// <summary> /// The class constructor. </summary> public SomeClass() { // TODO: Add Constructor Logic here } /// <summary> /// Name property </summary> /// <value> /// A value tag is used to describe the property value</value> public string Name { get { return myName; } } /// <summary> /// Description for SomeMethod.</summary> /// <paramname="s"> Parameter description for s goes here</param> /// <seealsocref="String"> /// You can use the crefattribute on any tag to reference a type or member /// and the compiler will check that the reference exists. </seealso> public void SomeMethod(string s) { }
  • 65. XML Documentation /// <summary> /// Class level summary documentation goes here.</summary> /// <remarks> /// Longer comments can be associated with a type or member /// through the remarks tag</remarks> public class SomeClass { /// <summary> /// Store for the name property</summary> private string myName= null; /// <summary> /// The class constructor. </summary> public SomeClass() { // TODO: Add Constructor Logic here } /// <summary> /// Name property </summary> /// <value> /// A value tag is used to describe the property value</value> public string Name { get { return myName; } } /// <summary> /// Description for SomeMethod.</summary> /// <paramname="s"> Parameter description for s goes here</param> /// <seealsocref="String"> /// You can use the crefattribute on any tag to reference a type or member /// and the compiler will check that the reference exists. </seealso> public void SomeMethod(string s) { }
  • 66. XML Documentation /// <summary> /// Class level summary documentation goes here.</summary> /// <remarks> /// Longer comments can be associated with a type or member /// through the remarks tag</remarks> public class SomeClass { /// <summary> /// Store for the name property</summary> private string myName= null; /// <summary> /// The class constructor. </summary> public SomeClass() { // TODO: Add Constructor Logic here } /// <summary> /// Name property </summary> /// <value> /// A value tag is used to describe the property value</value> public string Name { get { return myName; } } /// <summary> /// Description for SomeMethod.</summary> /// <paramname="s"> Parameter description for s goes here</param> /// <seealsocref="String"> /// You can use the crefattribute on any tag to reference a type or member /// and the compiler will check that the reference exists. </seealso> public void SomeMethod(string s) { } <?xml version="1.0"?> <doc> <assembly> <name>xmlsample</name> </assembly> <members> <member name="T:SomeClass"> <summary> Class level summary documentation goes here. </summary> <remarks> Longer comments can be associated with a type or member through the remarks tag </remarks> </member> <member name="F:SomeClass.myName"> <summary> Store for the name property </summary> </member> <member name="M:SomeClass.#ctor"> <summary>The class constructor.</summary> </member> <member name="M:SomeClass.SomeMethod(System.String)"> <summary>Description for SomeMethod.</summary> <paramname="s"> Parameter description for s goes here</param> <seealsocref="T:System.String"> You can use the crefattribute on any tag to reference a type or member and the compiler will check that the reference exists. </seealso> </member> …. </doc>
  • 67. XML Documentation /// <summary> /// Class level summary documentation goes here.</summary> /// <remarks> /// Longer comments can be associated with a type or member /// through the remarks tag</remarks> public class SomeClass { /// <summary> /// Store for the name property</summary> private string myName= null; /// <summary> /// The class constructor. </summary> public SomeClass() { // TODO: Add Constructor Logic here } /// <summary> /// Name property </summary> /// <value> /// A value tag is used to describe the property value</value> public string Name { get { return myName; } } /// <summary> /// Description for SomeMethod.</summary> /// <paramname="s"> Parameter description for s goes here</param> /// <seealsocref="String"> /// You can use the crefattribute on any tag to reference a type or member /// and the compiler will check that the reference exists. </seealso> public void SomeMethod(string s) { } <?xml version="1.0"?> <doc> <assembly> <name>xmlsample</name> </assembly> <members> <member name="T:SomeClass"> <summary> Class level summary documentation goes here. </summary> <remarks> Longer comments can be associated with a type or member through the remarks tag </remarks> </member> <member name="F:SomeClass.myName"> <summary> Store for the name property </summary> </member> <member name="M:SomeClass.#ctor"> <summary>The class constructor.</summary> </member> <member name="M:SomeClass.SomeMethod(System.String)"> <summary>Description for SomeMethod.</summary> <paramname="s"> Parameter description for s goes here</param> <seealsocref="T:System.String"> You can use the crefattribute on any tag to reference a type or member and the compiler will check that the reference exists. </seealso> </member> …. </doc>
  • 68. XML Documentation •To build the XML Documentation sample –To generate the sample XML documentation, type the following at the command prompt: •cscXMLsample.cs/doc:XMLsample.xml –To see the generated XML, issue the following command: •type XMLsample.xml
  翻译: