Friday, July 10, 2009

Create xml Using Dom and Use xPath to read value from it.

try
{
XmlDocument xDoc = new XmlDocument();

xDoc.LoadXml("<Country></Country>"); //Created the Parent Node.

XmlNode xCity = xDoc.CreateNode(XmlNodeType.Element, "City", "");
XmlNode xZoneNode = xDoc.CreateNode(XmlNodeType.Element, "Zone", "");
XmlNode xAreaNode = xDoc.CreateNode(XmlNodeType.Element, "Area", "");
XmlAttribute xCountryName = xDoc.CreateAttribute(XmlNodeType.Attribute.ToString(), "Name", "");
XmlNode xCityName = xDoc.CreateNode(XmlNodeType.Element, "Name", "");
xCountryName.Value = "India";
xCityName.InnerText = "Ahmedabad";
xAreaNode.InnerText = "Ahm";

xCity.AppendChild(xCityName);
xZoneNode.AppendChild(xAreaNode);
xCity.AppendChild(xZoneNode);

xDoc.DocumentElement.AppendChild(xCity);

xCity = xDoc.CreateNode(XmlNodeType.Element, "City", "");
xZoneNode = xDoc.CreateNode(XmlNodeType.Element, "Zone", "");
xAreaNode = xDoc.CreateNode(XmlNodeType.Element, "Area", "");
xCityName = xDoc.CreateNode(XmlNodeType.Element, "Name", "");

xCityName.InnerText = "Banglore";
xAreaNode.InnerText = "Bgr";

xCity.AppendChild(xCityName);
xZoneNode.AppendChild(xAreaNode);
xCity.AppendChild(xZoneNode);

xDoc.DocumentElement.AppendChild(xCity);

xCity = xDoc.CreateNode(XmlNodeType.Element, "City", "");
xZoneNode = xDoc.CreateNode(XmlNodeType.Element, "Zone", "");
xAreaNode = xDoc.CreateNode(XmlNodeType.Element, "Area", "");
xCityName = xDoc.CreateNode(XmlNodeType.Element, "Name", "");

xCityName.InnerText = "Pune";
xAreaNode.InnerText = "pn";

xCity.AppendChild(xCityName);
xZoneNode.AppendChild(xAreaNode);
xCity.AppendChild(xZoneNode);

xDoc.DocumentElement.AppendChild(xCity);




xDoc.DocumentElement.Attributes.Append(xCountryName);


//See ur out put here
string outPut = xDoc.DocumentElement.OuterXml;

//-------- This will Give output as ' ---------------//
////< Country Name="India" >
//// < City>
//// < Name >Ahmedabad
//// < Zone >
//// < Area >Ahm< /Area >
//// < /Zone >
//// < /City >
//// < City >
//// < Name >Banglore< /Name >
//// < Zone >
//// < Area >Bgr< /Area >
//// < /Zone >
//// < /City >
//// < City >
//// < Name >Pune
//// < Zone >
//// < Area>pn
//// < /Zone >
////
////

//now to query this Document Using the xPath u can do something like this

string nameParam = "Pune";
//get city name which is dependent upon the given area (set in areaParam)
string cityName = xDoc.DocumentElement.SelectSingleNode("City[@Name='"+nameParam+"']/Zone/Area").InnerText;


//-------- This will Give output as pn' ---------------//
}
catch (Exception e)
{ }

7 comments:

  1. Hi ........

    I am reading xml file i can find the path using reflection assembly i can read it........

    My Problem is .........
    I need how to write xml file using device application in c#.net

    I can't find the path whenever i am writing xml file or any textfile also..........

    Please help me ............

    ReplyDelete
  2. Smart Device Appplication (C#.net)

    I need to get Location or path for writing xml file that is my big problem...........

    whenever i am writing xml file i can't get path and i can not write xml or text file using smart device application............

    ReplyDelete
  3. hello kavita
    i want to genrate a xml file and bind xml to treeview structure in asp.net.

    in my sql database i am having string like
    \\SDCard\Blackberry\123.jpg
    \\SDcard\Blackberry\Videos\special\my.wmv
    \\SDcard\Blackberry\Music\everybody.m3

    how can be the xml created
    can u please help me..
    thanks in advance

    ReplyDelete
  4. Hi Kavitha,
    I am sathya web developer. I want to generate xml gallery dynamically using aspx coding. Xml generate format is

    /*gallery title="" thumbDir="./upload/thumbs/" imageDir="./upload/" random="true"
    category name="Introducing Seattle"
    image
    title View of Seattle /title
    desc View of Seattle /desc
    thumb seattle5.jpg /thumb
    img seattle5.jpg /img
    /image
    /category
    category name="Introducing Seattle"
    image
    title View of Seattle /title
    desc View of Seattle /desc
    thumb seattle5.jpg /thumb
    img seattle5.jpg /img
    /imag>
    /category
    /gallery*/..
    This is my xml gallery tags. I can add many category with xml and also i can upload many photos for any of the category. I created upload part and database with this category, but i do no how to generate xml for this category. Can u help me for this problem.......

    ReplyDelete
  5. Hi Kavitha,
    I'm Gianluca. I want save the xml above in a xml file (for example CountryCity.xml), this can be possible?

    ReplyDelete
    Replies
    1. I've solved the problem with this code:
      xDoc.Save("C:/Users/Gianluca/Documents/Visual Studio 2010/Projects/WebApplication1/WebApplication1/Xml/Prova.xml");

      Delete
  6. Thank You gianlu123 for saying about location to save. It works properly

    ReplyDelete