Monday, February 23, 2009

How to read entity value from Xml File .NET?

Please refer regarding entities at
How to create an entity in xml file at Prog. Findings
How to create an entity in xml file using .NET functions at Prog. Findings

As said about entities in above links, each entity defined in an xml file can be identified and referred using entity name.
Ex:-


&Entity1; in an inner text of a xml tag says Desc when it is loaded.

So to read these entity values we can refer them using there respective names.
Below function is to read entity value for a given entity name.
public string GetEntityValue(string strEntityName)
{
if (_xmlDocument != null)
{
return _xmlDocument.DocumentType.Entities.GetNamedItem(strEntityName).InnerText;
}
return String.Empty;
}

Below function is to get Entity name based upon given entity value.

public string GetEntityName(string strEntityValue)
{
string strEntityName = String.Empty;
if (_xmlDocument != null)
{
for (int i = 0; i < _xmlDocument.DocumentType.Entities.Count; i++)
{
if(_xmlDocument.DocumentType.Entities.Item(i).InnerText.Trim()==strEntityValue.Trim())
return _xmlDocument.DocumentType.Entities.Item(i).Name;
}
}
return strEntityName;
}
Below function is to get a lost all available entities with in an xml file.

public ArrayList GetEntityNames()
{
ArrayList objListEntities = new ArrayList();
if(_xmlDocument!=null)
{
for (int i = 0; i < _xmlDocument.DocumentType.Entities.Count; i++)
{
objListEntities.Add(_xmlDocument.DocumentType.Entities.Item(i).Name);
}
objListEntities.Sort();
}
return objListEntities;
}

This function returns array list which can further be used to bind list of values to drop down list.

No comments: