XML learning point drop

xiaoxiao2021-04-07  362

1.XML is built in the NET program, so it is a tree tree. In this tree, the nodes of all XML files are expressed as class. However, these classes represent different classes based on different XML node types. For example: Xmlelement, Xmlattribute, etc., and their common abstract class is XMLNode. They are the following relationship XMLELEMENT: XMLLINKEDNODE: XMLNode.

2. Read the value of a node in the XML file and modify it, XML is as follows:

beijing

Shanghai

tianjin

code show as below

XmLDocument Doc = New XmLDocument ();

Doc.Load ("Your.xml");

XMLNode Node;

Xmlelement root = doc.documentelement;

Node = root.selectsinglenode ("Users / NewTab / NewName);

Node.Value = "newvalue";

Doc.save ()

Or as follows

String filepath = httpContext.current.server.mappath ("Your.xml");

XMLDocument Xmldoc = new xmldocument ();

XMLDoc.Load (FilePath);

// Take the value

String XP = xmldoc.selectsinglenode ("newTab"). ChildNodes [0] .INNNERTEX;

// Give value

XMLDoc.selectsinglenode ("newTab"). ChildNodes [0] .INNNERTEX = "asdasd";

XMLDoc.save (filepath);

3. About Appendchild in XML

System.xml.xmldocument userinfo = new xmldocument ();

UserInfo.LoadXml (" ");

XMLNode Node = UserInfo.lastchild;

Xmlelement Elem = UserInfo.createElement ("pass");

ELEM.INNERTEXT = "pa";

XMLNode Node1 = UserInfo.createnode (XMLNodetyPe.element, "111", "");

XMLNode Node2 = UserInfo.createnode (XMLNodetype.element, "222", "");

XMLNode Node3 = UserInfo.createnode (XMLNodettype.element, "333", "");

Node.Appendchild (Node1);

Node.Appendchild (Node2);

Node.Appendchild (Node3); Foreach (XMLNode Node In Node)

Node.Appendchild (ELEM);

Userinfo.save (Server.MAppath ("1.xml"));

Got as follows:

<111>

PA

<222>

PA

<333>

PA

4. Modify the node name

Change to

That is to change A to B, but the following sub-node content is unchanged. code show as below:

First find a node Node first with SelectNode or other ways

XMLNode NodeTemp = Doc.createnode (Node.NodeType, "B", "");

NodeTemp.innerxml = node.innerxml;

Element.Replacechild (NodeTemp, Node);

转载请注明原文地址:https://www.9cbs.com/read-132431.html

New Post(0)