现有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></StartPoint>
</BeforeOtRange>
<HolidayWorkingType>
<Start>
<DateTime xsi:nil="true" />
</Start>
<End>
<DateTime xsi:nil="true" />
</End>
<StartPoint></StartPoint>
</HolidayWorkingType>
<AfterOtRange>
<Start>
<DateTime xsi:nil="true" />
</Start>
<End>
<DateTime xsi:nil="true" />
</End>
<StartPoint></StartPoint>
</AfterOtRange>
<OvernightIn xsi:nil="true" />
<OvernightOut xsi:nil="true" />
<OvernightDayCount></OvernightDayCount>
<TimeScale></TimeScale>
<TimeScaleType></TimeScaleType>
<IsAutoSwitchToOt>false</IsAutoSwitchToOt>
<SwitchType>-</SwitchType>
<IsSwitchTypeAfter>false</IsSwitchTypeAfter>
<IsSwitchTypeMiddle>false</IsSwitchTypeMiddle>
<IsSwitchTypeBefore>false</IsSwitchTypeBefore>
<EffectiveMonth></EffectiveMonth>
<IsSwitchToHoliday>false</IsSwitchToHoliday>
<Boundary xsi:nil="true" />
</Ot>
<Item>
<IsHolidayWorkingType>false</IsHolidayWorkingType>
<ClockInInterval xsi:nil="true" />
<WorkingHoursType></WorkingHoursType>
<WorkingHours>--01T00::</WorkingHours>
<EffectiveWorkingHours>--01T00::</EffectiveWorkingHours>
<WorkingDays></WorkingDays>
<IsNoAbsence>false</IsNoAbsence>
<LatePoint></LatePoint>
<LateBoundary xsi:nil="true" />
<LeaveEarlyPoint></LeaveEarlyPoint>
<LeaveEarlyBoundary xsi:nil="true" />
<WorkingHoursTimeScale></WorkingHoursTimeScale>
<MealMinuteCount></MealMinuteCount>
<SecondDayWorkingLazy xsi:nil="true" />
<MorningHalfDayStartDate>--01T00::</MorningHalfDayStartDate>
<MorningHalfDayEndDate>--01T00::</MorningHalfDayEndDate>
<AfterHalfDayStartDate>--01T00::</AfterHalfDayStartDate>
<AfterHalfDayEndDate>--01T00::</AfterHalfDayEndDate>
<LateAsAbsenteeismDate xsi:nil="true" />
<LeaveEarlyAsAbsenteeismDate xsi:nil="true" />
<IsLackSwingCard>false</IsLackSwingCard>
<DaysOfLackSwingCardAsabsenteeism></DaysOfLackSwingCardAsabsenteeism>
<Priority></Priority>
<IsToLateOrEarlyFromHalfDayOfAbsence>false</IsToLateOrEarlyFromHalfDayOfAbsence>
<YesterdayStart>--01T00::+:</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.net\example\XmlExample\1.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 = ;
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("\r\nPress any key to continue....");
Console.Read();
} }
}
}