C#-xml file Extract string + string Save as XML file + Create XML (custom node) file + Read node content

Source: Internet
Author: User

one, write the string to the XML file (and save)
Write:
XmlDocument Xdoc = new XmlDocument ();
Xdoc. LOADXML ("xmlstring");
Save:
Xdoc. Save ("Pathsave.xml")

ii. Assigning an XML file to a string
To read an XML file:
XmlDocument Xdoc = new XmlDocument ();
Xdoc. Load ("Pathload.xml");
return string:
Xdoc. Innerxml:xmlstring is your XML string

iii. Creating an XML file from a node
XmlTextWriter myXmlTextWriter = new XmlTextWriter (System.Web.HttpContext.Current.Server.MapPath (Path), SYSTEM.TEXT.ENCODING.UTF8);

myXmlTextWriter.Formatting = formatting.indented;

Myxmltextwriter.writestartdocument (FALSE);

Myxmltextwriter.writestartelement ("Respond"); Root node

Myxmltextwriter.writestartelement ("Head"); Head node

Myxmltextwriter.writeelementstring ("Return_code", data value); The content node of the head node
Myxmltextwriter.writeelementstring ("Return_desc", data value);
Myxmltextwriter.writeelementstring ("return_msg", data value);

Myxmltextwriter.writeendelement (); End of head node
Myxmltextwriter.writeendelement ();//root node end

Myxmltextwriter.flush ();
Myxmltextwriter.close ();

Iv. reading XML files by node
1. Create the Respond entity class First: (String * get;set;)
Content includes
Sting Return_code,return_msg,return_desc

/* * 获取RespondLog日志信息 * */public List<Respond> GetRespondLogXML(string path){    XmlDocument doc = new XmlDocument();    doc.Load(System.Web.HttpContext.Current.Server.MapPath(path));    XmlNode xn = doc.SelectSingleNode("Respond");    XmlNodeList xnl = xn.ChildNodes;    List<Respond> logList = new List<Respond>();    foreach (XmlNode xn1 in xnl)    {        Respond log = new Respond();        XmlElement xe = (XmlElement)xn1;        XmlNodeList xnl0 = xe.ChildNodes;        log.Return_code = xnl0.Item(0).InnerText;        log.Return_msg = xnl0.Item(1).InnerText;        log.Return_desc = xnl0.Item(2).InnerText;        logList.Add(log);    }    return logList;}

//Read Respond.xml file information
List loglist = new list ();
Loglist = Getrespondlogxml (Respondlog);
Return_code = loglist[0]. Return_code;
Return_msg = loglist[0]. return_msg;
Return_desc = loglist[0]. Return_desc;

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C#-xml file Extract string + string Save as XML file + Create XML (custom node) file + Read node content

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.