Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Friday, May 2, 2014

Write New or Existing XML in C#

Suppose i need to create XMLPath = @"c:\data\xmlfile.xml";


if (!File.Exists(XMLPath))
{
        //If XML File does not exist then Its creating XML from below method
        File.Create(XMLPath);
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        using (XmlWriter writer = XmlWriter.Create(XMLPath, settings))
        {
                writer.WriteStartDocument();
                writer.WriteComment("This file is generated by My Custom Tool.");
                writer.WriteStartElement("root");
                        //This is for child nodes
                        writer.WriteStartElement("childnode");
                        writer.WriteAttributeString("name", GameName);
                        writer.WriteAttributeString("path", XMLPath); 
                        writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Flush();
        }
}
else
{
        XmlDocument doc = new XmlDocument();
        doc.Load(XMLPath);
        XmlNodeList rootNodeList = doc.GetElementsByTagName("childnode");
        //Now Checking If node already exist
        bool nodeFound = false;
        foreach (XmlNode nd in rootNodeList)
        {
                    if (nd.Attributes["name"].Value.ToLower() == GameName.ToLower())
                    {
                          nd.Attributes["path"].Value = XMLPath;
                          doc.Save(XMLPath);
                          nodeFound = true;
                    }
        }
        if (nodeFound) return;
        //If Node doesn't exist already then 
        XmlNode root = doc.DocumentElement;
        XmlElement elem = doc.CreateElement("childnode");
        XmlAttribute nameAttr = doc.CreateAttribute("name");
        XmlAttribute gameAttr = doc.CreateAttribute("path");
        nameAttr.Value = GameName;
        gameAttr.Value = XMLPath;
        elem.Attributes.Append(nameAttr);
        elem.Attributes.Append(gameAttr);
        package.InsertBefore(elem, root.FirstChild);
        doc.Save(XMLPath);
}

It will create XML like below

<root>
      <childnode name="1" path="somename.xml"/>
      <childnode name="2" path="someothername.xml"/>
</root>

Wednesday, April 30, 2014

Easy way to search attribute value in XML

If there are many attributes with same name like below
<root>
      <node name="n1" path="1.jpg"/>
      <node name="n2" path="2.jpg"/>
      <node name="n3" path="3.jpg"/>
      <node name="n4" path="4.jpg"/>
</root>

Then if you want to search node n3 path value in easy method then use the following method

XmlDocument doc = new XmlDocument();
doc.Load(xmlPath);
doc.SelectSingleNode("/root/node[@name='n3']").Attributes["path"].Value

Tuesday, April 15, 2014

Syntax Formatting for any Language in WPF C# Textbox

Its really Simple and Easy to format any languge
  1. First You need to install AvalonEdit from Manage NuGet Packages. Use following link to install instructions http://www.nuget.org/packages/AvalonEdit
or Just right click on Project in Solution Explorer and Click Manage NuGet Packages, now click Online and Search Avalon Editor. Then Install AvalonEditor
  1. Just add following code in xaml file.
    xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit"
  2. Now add your TextEditor as follow
    <avalonedit:TextEditor SyntaxHighlighting="XML" x:Name="gameListXMLText" Height="200">
  3. Just Change SyntaxHighlighting as you want it could be C#
  4. Now Just load xml file as follow
gameListXMLText.Text = File.ReadAllText("sample.xml");

Wednesday, March 12, 2014

How to read XML in easy way in Win C#

I have an XML like below

<?xml version="1.0" encoding="UTF-8"?>
<xd:xmldiff xmlns:xd="http://schemas.microsoft.com/xmltools/2002/xmldiff" fragments="no" options="IgnoreChildOrder IgnoreNamespaces IgnorePrefixes " srcDocHash="41SGFJBSJ43294" version="1.0">
      <xd:node match="2">
            <xd:change match="@version">5.3.7.0</xd:change>
            <xd:node match="3">
                  <xd:change match="@market">default</xd:change>
            </xd:node>
            <xd:node match="14">
                  <xd:node match="8">
                  <xd:change match="@iconFilename">Icons/fav.ico</xd:change>
            </xd:node>
      </xd:node>
 </xd:xmldiff>

Now to read this. (If this is in in some file)
use like below
first of all add:
using System.Xml;

Now write the below code 
XmlDocument doc1 = new XmlDocument();
doc1.Load(xmlFilePath);

And then 
XmlNodeList rootNodeList = doc1.GetElementsByTagName("xd:change");
foreach (XmlNode nd in rootNodeList)
{
  if(nd.ChildNodes.Count>0)
    Console.WriteLine(nd.Attributes["match"].Value + ", " + nd.ChildNodes.Item(0).Value);
  else
    Console.WriteLine(nd.Attributes["match"].Value);
}

Above code will show match attribute of all "xd:change" node. Either that is Parent Node or Child Node.