C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » XML »

Xml parser


Posted Date: 26 Jul 2009    Resource Type: Code Snippets    Category: XML
Author: muralikrishnanMember Level: Silver    
Rating: 1 out of 5Points: 7



Description :


This is the dll which can parse the whole xml file and can manipulate the node values in the xml file provided as input.

Here is the code:

using System;
using System.Data;
using System.Configuration;
using System.Xml;
using System.Text;

namespace XML_Parser
{
public class ParsingXMLFile
{
private string strElement;
private string strAttributeName;
private string strAttributeValue;
private string strAttribute;

///
/// Reads the xml file .
///

/// Xml file path.
/// String array which contains all the element names in the specified file name.
public string[] ReadXmlFile(string strpath)
{
try
{
//Initializes Xml Reader
XmlTextReader textReader = new XmlTextReader(strpath);

//Loop through each element in the xml file.
while (textReader.Read())
{
XmlNodeType nType = textReader.NodeType;

//if node type is element gets that node's name.
if (nType == XmlNodeType.Element)
{
strElement = strElement + textReader.Name.ToString() + ":";
}

}
//string array to store all the elements in the xml file path.
string[] strElementArr;
strElementArr = strElement.Split(':');

//Clearing the xml reader instance.
textReader.Close();

return strElementArr;
}
catch (Exception err)
{
//Error handler to trap the errors.
string[] strErr = new string[1];
strErr[0] = err.Message;
return strErr;
}

}

///
/// Appends child nodes to the parent node specified
///

/// Parent node name to which new attribute to be added.
/// Child node name to be appended.
/// New Child node value
/// Xml file path
///
/// True . if node appended successfully.
/// False . if appending node failed.
///

public bool AppendChild(string strParentName, string strChildName,string strChildValue,string strpath)
{
try
{
if (strParentName != string.Empty)
{
//Initializing Xml document to load the Xml file
XmlDocument doc = new XmlDocument();
doc.Load(strpath);

//Creating child node
XmlNode node = doc.CreateNode(XmlNodeType.Element, strChildName, null);
node.InnerText = strChildValue;

//Gets the parent node in the node list
XmlNodeList nodelist = doc.GetElementsByTagName(strParentName);

//Appends the child node to the parent node.
nodelist[0].AppendChild(node);

//Saves the xml document
doc.Save(strpath);

}
return true;
}

catch (Exception)
{
return false;
}
}

///
/// Removes child node from the specific parent node
///

/// Parent name from which the node to be removed.
/// Child name to be removed.
/// Xml file path.
///
/// True . If Node removed successfully.
/// False . If node deletion failed.
///

public bool RemoveChild(string strParentName, string strChildName, string strpath)
{
try
{
//Initializing Xml document to load the Xml file
XmlDocument doc = new XmlDocument();
doc.Load(strpath);

//Gets the parent node name in the xml file.
XmlNode root = doc.SelectSingleNode("//" + strParentName);

//Parent node has child node under it's name
if (root.HasChildNodes)
{
//Gets the child node from the xml file.
XmlNode child = root.SelectSingleNode("//" + strChildName);

//Removes the child node
root.RemoveChild(child);

//Saves the xml document.
doc.Save(strpath);
}
return true;

}
catch (Exception)
{
return false;
}
}

///
/// Retrieves all the attributes of the spefied parent node.
///

/// Parent node name for which attributes value to be retrieved.
/// Xml file path
///
/// Jagged array with two elements[Two single dimensional array in it].
/// First Array : contains the atrributes name for the node specified.
/// Second array : contains the atrributes value for the node specified.
///

public string[][] GetAttribute(string strParentName, string strpath)
{
try
{

//Initializing Xml document to load the Xml file
XmlDocument doc = new XmlDocument();
doc.Load(strpath);

//Gets the parent node name in the xml file.
XmlNode root = doc.SelectSingleNode("//" + strParentName);

//Gets the attributes respective to the parent node.
XmlAttributeCollection attrcol = root.Attributes;

for (int i = 0; i < attrcol.Count; i++)
{
//string to stroe the attribute names.
strAttributeName = strAttributeName + root.Attributes[i].Name + ":";
//string to strore the attribute values.
strAttributeValue = strAttributeValue + root.Attributes[i].Value + ":";

}
//Concatenating atrribute name and values to the single string with the seperator '/'
strAttribute = strAttributeName + "/" + strAttributeValue;

//string array to store the attributes name and values.
string[] strAttributeArr = strAttribute.Split('/');

//Creating Jagged arry with two elements
string[][] strJaggedArray = new string[2][];

//Stores the attribute names in the first element for the jagged array
strJaggedArray[0]=strAttributeArr[0].Split(':');

//Stores attribute values in the second element of the jagged array.
strJaggedArray[1]=strAttributeArr[1].Split(':');


return strJaggedArray;
}

catch (Exception)
{
string[][] strJaggedErr = new string[2][];
return strJaggedErr;

}
}
///
/// Add attributes to the parent node specified
///

/// Parent node name to which new attribute to be added.
/// New attribute's name
/// New attributes value
/// Xml file path
///
/// True.if attributes added successfully.
/// false.if adding attribute failed.
///

public bool AddAttribute(string strParentName, string strAttributeName,string strAttributevalue, string strpath)
{
try
{
//Initializing Xml document to load the Xml file
XmlDocument doc = new XmlDocument();
doc.Load(strpath);

//Gets the parent node from the xml document
XmlNode node = doc.SelectSingleNode("//" + strParentName);

//Creates new attribute
XmlAttribute newattr = doc.CreateAttribute(strAttributeName);
newattr.Value = strAttributevalue;

//Appends the attribute to the parent node.
node.Attributes.Append(newattr);

//Saves the xml document
doc.Save(strpath);

return true;
}
catch (Exception )
{
return false;
}
}


///
/// Removes attribute from the specified parent node.
///

/// Parent node name from which the attributes to be removed.
/// Child node name which is going to be removed.
/// Xml file path.
///
/// True . If Attributes removed successfully.
/// False . If attributes deletion failed.
///

public bool RemoveAttribute(string strParentName,string strChildNameToRemove , string strpath)
{
try
{
//Initializing Xml document to load the Xml file
XmlDocument doc = new XmlDocument();
doc.Load(strpath);

//Gets the parent node from the xml document
XmlNodeList list = doc.GetElementsByTagName(strParentName);

//Removes the attribute from the parent node.
foreach (XmlNode node in list)
{
node.Attributes.RemoveNamedItem(strChildNameToRemove);
}

//Saves the xml document
doc.Save(strpath);
return true;
}

catch (Exception)
{
return false;
}
}

///
/// Changes name or value of the specific attribute.
///

/// Parent node name to which new attribute to be added.
/// Attribute name which to be updated.
/// New Attribute Name.
/// New Attribute value.
/// XML file path
///
/// True . If attribute updated successfully.
/// False. If attribute updation failed.
///

public bool ChangeAttribute(string strParentName, string strAttrToChange, string strNewAttrName, string strNewAttrValue, string strpath)
{
try
{
//Initializing Xml document to load the Xml file
XmlDocument doc = new XmlDocument();
doc.Load(strpath);

//Gets the parent node from the xml document
XmlNodeList list = doc.GetElementsByTagName(strParentName);

//Removes the attribute from the parent node.
foreach (XmlNode node in list)
{
node.Attributes.RemoveNamedItem(strAttrToChange);
}

//Gets the parent node from the xml document
XmlNode nodeChange = doc.SelectSingleNode("//" + strParentName);

//Creates new attribute
XmlAttribute newattr = doc.CreateAttribute(strNewAttrName);
newattr.Value = strNewAttrValue;

//Appends the attribute to the parent node.
nodeChange.Attributes.Append(newattr);

//Saves the xml document
doc.Save(strpath);
return true;
}
catch (Exception)
{
return false;
}
}
}
}



Attachments

  • xml parser component (30760-26930-ParsingXMLFile.cs.txt)


  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    Xml parser  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: SALT Listen element
    Previous Resource: Writing in XML
    Return to Discussion Resource Index
    Post New Resource
    Category: XML


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use