现有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>
我要去除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(); } } } }