c\#xml 읽 기
<?xml version="1.0" encoding="utf-8"?>
<PersonF xmlns="" Name="(test)work hard work smart!">
  <person Name="Person1">
    <ID>1</ID>
    <Name>XiaoA</Name>
    <Age>59</Age>
  </person>
  <person Name="Person2">
    <ID>2</ID>
    <Name>XiaoB</Name>
    <Age>29</Age>
  </person>
  <person Name="Person3">
    <ID>3</ID>
    <Name>XiaoC</Name>
    <Age>103</Age>
  </person>
  <person Name="Person4">
    <ID>4</ID>
    <Name>XiaoD</Name>
    <Age>59</Age>
  </person>
</PersonF> 
  
 
Code:       。
 
?
public  void TestXML()
{
    XmlDocument doc = new XmlDocument();
    string path = "http://www.cnblogs.com/MyDocument/Person.xml";
    try
    {
 
        //doc.Load(Server.MapPath()
        doc.Load(path);
 
        //1、         
        XmlNode node = doc.SelectSingleNode("PersonF");
 
        //2、         
        XmlNodeList nodeList1 = doc.SelectNodes("PersonF/person");
 
        //3.1             :   Person2      Name InnerText
        XmlNodeList nodeList = doc.DocumentElement.GetElementsByTagName("person");
        foreach (XmlNode node2 in nodeList1) //     nodeList  
        {
            if (node2.Attributes["Name"].InnerText == "Person2")
            {
                Console.WriteLine(node2.ChildNodes[1].InnerText);
 
            }
        }
 
        //3.2   ID 2           Name InnerText
        XmlNode node3 = doc.SelectSingleNode("PersonF/person[ID=2]");
        string strNode3 = node3.ChildNodes[1].InnerText;
 
        //3.3           ID 2   
        XmlNodeList nodeList2 = doc.SelectNodes("//person//ID");
        XmlNode node4 = null;
        foreach (XmlNode node5 in nodeList2)
        {
            if (node5.InnerText == "2")
            {
                node4 = node5;
                break;
            }
        }
        Console.WriteLine(node4.InnerText);
 
        //4、       
        string Name = node.Attributes["Name"].InnerText;
 
        //5        
        node.Attributes["Name"].InnerText = "work hard work smart!";
        doc.Save(path);
 
        //6         
        XmlTextReader reader = new XmlTextReader(path);
        XmlElement root = doc.DocumentElement;//     
        XmlElement tagOuter = doc.CreateElement("person");
        //      
        tagOuter.SetAttribute("Name", "Person5");
        XmlElement tagIN_Name = doc.CreateElement("Name");
        XmlElement tagIN_ID = doc.CreateElement("ID");
        tagIN_Name.InnerText = "work hard work smart!";
        tagIN_ID.InnerText = "32";
        tagOuter.AppendChild(tagIN_Name);
        tagOuter.AppendChild(tagIN_ID);
        root.AppendChild(tagOuter);//  tagOuter XML     
        reader.Close();
        doc.Save(path);
    }
    catch (System.Exception e)
    {
        throw new Exception(e.Message);
    }
} 
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.