Dotnet Summary (4)--xml reading and writing

Source: Internet
Author: User
Tags foreach config count dotnet
XML read XML,
public static string Readxmlconfig (String strFileName, String SKey)
{
String strFileName = "Brand.exe.config";
XmlDocument oxmldocument = new XmlDocument ();

String Strreturn= "";
Try
{
Oxmldocument.load (application.startuppath+ "\" +strfilename);
XmlNodeList oxmlnodelist = oXmlDocument.DocumentElement.ChildNodes;
foreach (XmlElement oxmlelement in oxmlnodelist)
{
if (oXmlElement.Name.ToLower () = = "appsettings")
{
XmlNodeList _node = oxmlelement.childnodes;
if (_node. Count > 0)
{
foreach (XmlElement _el in _node)
{
if (_el. attributes["Key"]. Innerxml.tolower () = = Skey.tolower ())
{
Strreturn = _el. attributes["Value"]. Value;
Break
}
}
}
}
}
}
catch (Exception exp)
{
if (skey.tolower () = "DSN")
{
Strreturn = "Connect timeout=5;server=localhost;database=brand;uid=sa;pwd=";
}

XmlTextWriter oxmltextwriter = new XmlTextWriter (strFileName, New System.Text.UTF8Encoding ());
Oxmltextwriter.writestartdocument ();
Oxmltextwriter.writestartelement ("Configuration");
Oxmltextwriter.writestartelement ("appSettings");
Oxmltextwriter.writestartelement ("add");
Oxmltextwriter.writeattributestring ("Key", SKey);
Oxmltextwriter.writeattributestring ("value", strreturn);
Oxmltextwriter.writeendelement ();
Oxmltextwriter.writeendelement ();
Oxmltextwriter.writeendelement ();
Oxmltextwriter.writeenddocument ();
Oxmltextwriter.close ();
}
return strreturn;
}


Write
private string Writewebconfig (String SKey, String svalue)
{
String strFileName = "Brand.exe.config";
XmlDocument oxmldocument = new XmlDocument ();
Try
{
Oxmldocument.load (strFileName);
XmlNodeList oxmlnodelist = oXmlDocument.DocumentElement.ChildNodes;
foreach (XmlElement oxmlelement in oxmlnodelist)
{
if (oXmlElement.Name.ToLower () = = "appsettings")
{
XmlNodeList _node = oxmlelement.childnodes;
if (_node. Count > 0)
{
foreach (XmlElement _el in _node)
{
if (_el. attributes["Key"]. Innerxml.tolower () = = SKey)
{
_el. attributes["Value"]. Value = svalue;
Break
}
}
}
}
}
}
Catch
{
Return "";
}
Oxmldocument.save (strFileName);
return svalue;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.