Another new Wow from me on this topic. It was getting more fun and interesting on LINQ
programming. However, most of the things here can be learned from the hands on lab. I am
trying to grasp the idea behind LINQ by blogging it.
1)
Below is how you can create a simple xml document and display it on the standard output.
/// <summary>
/// Create a new Sample XML Document
/// </summary>
/// <returns></returns>
public static XDocument CreateDocument()
{return new XDocument(
new XDeclaration("1.0", null, null),new XElement("communities",new XElement("community",new XElement("SgDotNet", new XAttribute("scope", ".NET Technologies"),new XElement("Member", "Kit Kai"),new XElement("Member", "Justin Lee")),new XElement("SgDotNet", new XAttribute("scope", "MS Vista Technologies"),new XElement("Member", "Someone I know"),new XElement("Member", "Someone you know")),new XElement("MIND", new XAttribute("scope", "Microsoft Technologies"),new XElement("Member", "DirectX"),new XElement("Member", "SomeUser")) )
)
);
}
/// <summary>
/// Display the new XML Document in standard output / console
/// </summary>
public static void DisplayXmlDocument()
{XDocument document = CreateDocument();
Console.WriteLine(document);
Console.ReadLine();
}
2)
I will perform a basic query expression on getting SgDotNet members who are interested or
specialices in .NET Technologies.
/// <summary>
/// Retrieve SgDotNet Members which specializes in .NET Technologies
/// </summary>
public static void GetSgDotNetMembers()
{XDocument document = CreateDocument();
// Find through the SgDotNet element, and select only the scope specified here
var members =
from c in document.Descendants("SgDotNet")where (string)c.Attribute("scope") == ".NET Technologies"select c;
Console.WriteLine("Expecting Output:");foreach (var member in members)
{Console.WriteLine(member);
}
Console.ReadLine();
}
3)
However, you can retrieve a record from an existing XML file, and then pass this values to a
new set of XML.
Assuming you have called SaveXmlFile method in the Main() method or somewhere else. Take note, there
is a bug in this code below. I am not really aware of the LINQ syntax for time being, so do forgive me.
/// <summary>
/// Save the expected xml into the hard drive
/// </summary>
private static void SaveXmlFile()
{XDocument document = CreateDocument();
document.Save("Communities.xml");}
/// <summary>
/// Query the existing xml, get and store into a new set of xml
/// - Contains a bug in this code!
/// </summary>
public static void CreateNewSetOfXml()
{// Load from the xml file
XDocument document = XDocument.Load("Communities.xml");XElement newDocument = null;
// Find through the SgDotNet element, and select only the scope specified here
var members =
from c in document.Descendants("SgDotNet")where (string)c.Attribute("scope") == ".NET Technologies"select c;
//BUG: Suppose to be print 2 xml nodes, but only 1 - how come?
foreach (var member in members)
{newDocument =
new XElement("Communities",new XElement("Community", new XAttribute("Member", (string)member)));Console.WriteLine(newDocument);
}
Console.ReadLine();
}
Conclusion:
Well even though it is quite fun to work with LINQ, but it is hard to code it in vs 2005 without
proper intellisense. You can't really debug the codes in VS 2005 (mine is August edition).
Cheers.