• xml去除指定节点,保留子节点操作


    现有xml文件:

    <?xml version="1.0" encoding="utf-8" ?>
    <ShiftConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <Ot>
            <BeforeOtRange>
                <Start>
                    <DateTime xsi:nil="true" />
                </Start>
                <End>
                    <DateTime xsi:nil="true" />
                </End>
                <StartPoint>0</StartPoint>
            </BeforeOtRange>
            <HolidayWorkingType>
                <Start>
                    <DateTime xsi:nil="true" />
                </Start>
                <End>
                    <DateTime xsi:nil="true" />
                </End>
                <StartPoint>0</StartPoint>
            </HolidayWorkingType>
            <AfterOtRange>
                <Start>
                    <DateTime xsi:nil="true" />
                </Start>
                <End>
                    <DateTime xsi:nil="true" />
                </End>
                <StartPoint>0</StartPoint>
            </AfterOtRange>
            <OvernightIn xsi:nil="true" />
            <OvernightOut xsi:nil="true" />
            <OvernightDayCount>0</OvernightDayCount>
            <TimeScale>0</TimeScale>
            <TimeScaleType>0</TimeScaleType>
            <IsAutoSwitchToOt>false</IsAutoSwitchToOt>
            <SwitchType>-1</SwitchType>
            <IsSwitchTypeAfter>false</IsSwitchTypeAfter>
            <IsSwitchTypeMiddle>false</IsSwitchTypeMiddle>
            <IsSwitchTypeBefore>false</IsSwitchTypeBefore>
            <EffectiveMonth>0</EffectiveMonth>
            <IsSwitchToHoliday>false</IsSwitchToHoliday>
            <Boundary xsi:nil="true" />
        </Ot>
        <Item>
            <IsHolidayWorkingType>false</IsHolidayWorkingType>
            <ClockInInterval xsi:nil="true" />
            <WorkingHoursType>0</WorkingHoursType>
            <WorkingHours>1900-01-01T00:00:00</WorkingHours>
            <EffectiveWorkingHours>1900-01-01T00:00:00</EffectiveWorkingHours>
            <WorkingDays>0</WorkingDays>
            <IsNoAbsence>false</IsNoAbsence>
            <LatePoint>0</LatePoint>
            <LateBoundary xsi:nil="true" />
            <LeaveEarlyPoint>0</LeaveEarlyPoint>
            <LeaveEarlyBoundary xsi:nil="true" />
            <WorkingHoursTimeScale>0</WorkingHoursTimeScale>
            <MealMinuteCount>0</MealMinuteCount>
            <SecondDayWorkingLazy xsi:nil="true" />
            <MorningHalfDayStartDate>1900-01-01T00:00:00</MorningHalfDayStartDate>
            <MorningHalfDayEndDate>1900-01-01T00:00:00</MorningHalfDayEndDate>
            <AfterHalfDayStartDate>1900-01-01T00:00:00</AfterHalfDayStartDate>
            <AfterHalfDayEndDate>1900-01-01T00:00:00</AfterHalfDayEndDate>
            <LateAsAbsenteeismDate xsi:nil="true" />
            <LeaveEarlyAsAbsenteeismDate xsi:nil="true" />
            <IsLackSwingCard>false</IsLackSwingCard>
            <DaysOfLackSwingCardAsabsenteeism>0</DaysOfLackSwingCardAsabsenteeism>
            <Priority>0</Priority>
            <IsToLateOrEarlyFromHalfDayOfAbsence>false</IsToLateOrEarlyFromHalfDayOfAbsence>
            <YesterdayStart>1900-01-01T00:00:00+08:00</YesterdayStart>
        </Item>
    </ShiftConfig>
    View Code

    我要去除Ot与Item节点:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    using System.Web;
    namespace ConsoleApplicationXml
    {
        class Program
        {
            static void Main(string[] args)
            {
                //string xmlFilePath = @"X:about.netexampleXmlExample1.xml";
                //string xmlFilePath = HttpContext.Current.Server.MapPath("ShiftConfig.xml");
                //StringBuilder sb = new StringBuilder();
                if (File.Exists("ShiftConfig.xml") == true)
                {
                    string xmlValue = "";
                    string xmlString = File.ReadAllText("ShiftConfig.xml");
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.LoadXml(xmlString);  
                    XmlNode root = xmlDoc.DocumentElement;     //取到根结点
                    XmlNode otNode = xmlDoc.SelectSingleNode("ShiftConfig/Ot");
                    XmlNode itemNode = xmlDoc.SelectSingleNode("ShiftConfig/Item");
                    if (otNode != null)
                    {
                        XmlNodeList otNodeList = otNode.ChildNodes;
                        if (otNodeList != null)
                        {
                            foreach (XmlNode xn in otNodeList)
                            {
                                XmlNode deep = xn.CloneNode(true); //克隆根节点 
                                XmlNode xx = xmlDoc.ImportNode(deep, true);
                                root.AppendChild(xx);
                            }
                            root.RemoveChild(otNode);
                        }
                    }
                    if (itemNode != null)
                    {
                        XmlNodeList itemNodeList = itemNode.ChildNodes;
                        if (itemNodeList != null)
                        {
                            foreach (XmlNode xn in itemNodeList)
                            {
                                XmlNode deep = xn.CloneNode(true); //克隆根节点 
                                XmlNode xx = xmlDoc.ImportNode(deep, true);
                                root.AppendChild(xx);
                            }
                            root.RemoveChild(itemNode);
                        }
                    }
    
                    if (otNode != null || itemNode != null)
                    {
                        MemoryStream stream = new MemoryStream();
                        XmlTextWriter writer = new XmlTextWriter(stream, null);
                        writer.Formatting = Formatting.Indented;
                        xmlDoc.Save(writer);
                        StreamReader streamReader = new StreamReader(stream, System.Text.Encoding.UTF8);
                        stream.Position = 0;
                        xmlValue = streamReader.ReadToEnd();
                        streamReader.Close();
                        stream.Close();
                    }
    
                    Console.WriteLine(xmlValue);
    
    
                    
                    //if (otNodeList != null)
                    //{ 
                    //    foreach (XmlNode xn in otNodeList)
                    //    { 
                    //        XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
                    //        //if (xe.Name == "BeforeOtRange")//如果找到
                    //        //{ 
                    //        //     
                    //        //} 
                    //        sb.Append("" + xe.OuterXml + "");
                    //    }  
                    //}
                    //if (itemNodeList != null)
                    //{ 
                    //    //TODO:把item下面的节点foreach
                    //    foreach (XmlNode xn in itemNodeList)
                    //    {
                    //        XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
                    //        sb.Append("" + xe.OuterXml + "");
                    //    } 
                    //}
                    //root.InnerXml = sb.ToString();
    
                    xmlDoc.Save("D:\xmloperate\ConsoleApplicationXml\ShiftConfig.xml"); 
                    //Console.WriteLine(sb.ToString());
                    //Console.WriteLine(root);
                    
                    Console.Write("
    Press any key to continue....");
                    Console.Read();
                }
    
    
                
                
            }
        }
    }
  • 相关阅读:
    Codeforces A. Bear and Big Brother
    codeforces A. In Search of an Easy Problem
    c#判断两个对象和对象中的属性是否相同(以及记录对象中的哪些字段,和详细的改变情况)
    生成随机字符串
    SQL语句计算距离今天生日还差几天
    sqlServer 获取最新的一条数据
    c#所有部门及其下所部门生成树形图(递归算法获取或键值对方式获取)
    根据中文名,自动生成首字母的拼音码或拼音码(两种方法)
    char/varchar/nvarchar的区别
    c#中ofType的用法
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/4955190.html
Copyright © 2020-2023  润新知