# Read XML node content method example let's take a look at the implementation of C # Read XML nodes:
- Using System;
- Using System. Xml;
- Using System. Xml. XPath;
- Using System. DaTa;
- Class ReadXML
- {
- Public static void Main ()
- {
- String sFile = "ReadXml. xml ";
- // C # Read the XML Node method 1
- XmlDocument doc = new XmlDocument ();
- Doc. Load (sFile );
- XmlNode node = doc. DocumentElement ["News"] ["Content"];
- Console. WriteLine (node. InnerText );
- // C # Read the XML node method2
- Node = doc. SelectSingleNode ("// Content ");
- Console. WriteLine (node. InnerText );
- // Similarly
- Node = doc. DocumentElement. SelectSingleNode ("News/Content ");
- Console. WriteLine (node. InnerText );
- // C # Read the XML Node method 3
- DataSet ds = new DataSet ();
- Ds. ReadXml (sFile );
- Console. WriteLine (ds. Tables [0]. Rows [0] ["Content"]. ToString ());
- // C # Read the XML Node method 4
- XmlTextReader reader = new XmlTextReader (sFile );
- While (reader. Read ())
- {
- If (reader. Name = "Content ")
- {
- Console. WriteLine ("***" + reader. ReadString ());
- Break;
- }
- }
- Reader. Close ();
- // C # Read the XML Node method 5
- XPathDocument xpdoc = new XPathDocument (sFile );
- XPathNavigator xpnv = xpdoc. CreateNavigator ();
- Xpnv. MoveToFirstChild ();
- Xpnv. MoveToFirstChild ();
- Xpnv. MoveToFirstChild ();
- Xpnv. MoveToNext ();
- Console. WriteLine ("pathnavigator:" + xpnv. Value );
- }
- }
My methods
string newQiChar=DateTime.Now .ToString ("yyMMdd");
string phyPath = AppDomain.CurrentDomain.BaseDirectory;
string name = "config.xml";
string configPath = Path.Combine(phyPath, name);
SaveConfigXml (configPath,newQi,newQiChar);
/// <Summary>
/// Save the configuration file
/// </Summary>
/// <Param name = "savePath"> file path </param>
/// <Param name = "qishu"> Number of int-type periods </param>
/// <Param name = "qishuChar"> Number of string periods </param>
/// <Returns> </returns>
Public static bool SaveConfigXml (string savePath, int qishu, string qishuChar)
{
Bool flag = false;
Try
{
XmlDocument doc = new XmlDocument ();
Bool exists = System. IO. File. Exists (savePath );
If (! Exists)
{// Generate
Doc. LoadXml ("<Configs> </Configs>"); // create the root node MaxQishu
XmlNode root = doc. SelectSingleNode ("Configs ");
XmlElement MaxQishu = doc. CreateElement ("MaxQishu ");
MaxQishu. SetAttribute ("id", "qishu ");
MaxQishu. SetAttribute ("value", qishu. ToString ());
MaxQishu. SetAttribute ("text", qishuChar );
Root. AppendChild (MaxQishu );
Doc. Save (savePath );
}
Else
{// Modify
Doc. Load (savePath );
XmlElement xe = (XmlElement) doc. SelectSingleNode ("Configs/MaxQishu ");
If (xe! = Null)
{
Xe. SetAttribute ("id", "qishu ");
Xe. SetAttribute ("value", qishu. ToString ());
Xe. SetAttribute ("text", qishuChar );
Doc. Save (savePath); // Save
}
}
flag = true;
}
catch (Exception ex)
{
flag = false;
}
return flag;
}
/// <Summary>
/// Number of read periods from the configuration file
/// </Summary>
/// <Param name = "savePath"> </param>
/// <Param name = "type"> 1-int number of digits, 0-string number of digits </param>
/// <Returns> </returns>
Public static object ReadMaxQishuFromXml (string savePath, int type)
{
Object result = string. Empty;
Try
{
XmlDocument doc = new XmlDocument ();
Bool exists = System. IO. File. Exists (savePath );
If (exists)
{// Exists
Doc. Load (savePath );
# Region
/*
String xpathPath = "";
If (type = 1)
{// Number of digits
XpathPath = "Configs/MaxQishu/value ";
}
Else
{
XpathPath = "Configs/MaxQishu/text ";
}
XmlNode node = (XmlNode) doc. SelectSingleNode (xpathPath );
Result = node. InnerText;
**/
# Endregion
XmlElement xe = (XmlElement) doc. SelectSingleNode ("Configs/MaxQishu ");
String name = xe. Name;
String value = xe. Value;
If (type = 1)
{
Result = xe. GetAttribute ("value ");
}
Else
{
Result = xe. GetAttribute ("text ");
}
}
}
catch (Exception ex)
{
}
return result;
}
}