如何在 C# 中使用 XMLDocument 删除 XML 文件的第一行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16036758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete first line of XML file using XMLDocument in C#?
提问by
I am reading an XML file in C# using XMLDocument. My code goes like this:
我正在使用 C# 读取 XML 文件XMLDocument。我的代码是这样的:
XmlDocument doc = new XmlDocument();
doc.Load(xmlSourceFile);
First line of my XML document is
我的 XML 文档的第一行是
<?xml version="1.0" encoding="UTF-8"?>
I have to delete this line. How should I?
我必须删除这一行。我该怎么办?
采纳答案by Matt
I don't see why you would want to remove that. But if it is required, you could try this:
我不明白你为什么要删除它。但如果需要,你可以试试这个:
XmlDocument doc = new XmlDocument();
doc.Load("something");
foreach (XmlNode node in doc)
{
if (node.NodeType == XmlNodeType.XmlDeclaration)
{
doc.RemoveChild(node);
}
}
or with LINQ:
或使用 LINQ:
var declarations = doc.ChildNodes.OfType<XmlNode>()
.Where(x => x.NodeType == XmlNodeType.XmlDeclaration)
.ToList();
declarations.ForEach(x => doc.RemoveChild(x));
回答by Ben Holland
I understand the need to eliminate the XML Declaration; I'm working on a script that alters the content of an application's preferences.xml, and the application doesn't read the file properly if the declaration is there (not sure why the developers decided to omit the XML declaration).
我理解消除 XML 声明的必要性;我正在编写一个改变应用程序内容的脚本,preferences.xml如果声明在那里,应用程序就不能正确读取文件(不知道为什么开发人员决定省略 XML 声明)。
Instead of messing around with the XML any longer, I just created a removeXMLdeclaration()method that reads the XML file and removes the first line, then rewrites it back simply using streamreaders/writers. It's lightning fast, and works great! I just call the method after I've done all my XML alteration to clean the file up once and for all.
我不再removeXMLdeclaration()纠结于 XML,而是创建了一个方法来读取 XML 文件并删除第一行,然后简单地使用流读取器/写入器将其重写回来。它闪电般快速,而且效果很好!我只是在完成所有 XML 更改后调用该方法以一劳永逸地清理文件。
Here is the code:
这是代码:
public void removeXMLdeclaration()
{
try
{
//Grab file
StreamReader sr = new StreamReader(xmlPath);
//Read first line and do nothing (i.e. eliminate XML declaration)
sr.ReadLine();
string body = null;
string line = sr.ReadLine();
while(line != null) // read file into body string
{
body += line + "\n";
line = sr.ReadLine();
}
sr.Close(); //close file
//Write all of the "body" to the same text file
System.IO.File.WriteAllText(xmlPath, body);
}
catch (Exception e3)
{
MessageBox.Show(e3.Message);
}
}
回答by Manjeet Singh
There is another way to close this file use file stream.
还有另一种方法可以关闭此文件使用文件流。
public void xyz ()
{
FileStream file = new FileStream(xmlfilepath, FileMode.Open, FileAccess.Read);
XmlDocument doc = new XmlDocument();
doc.load(xmlfilepath);
// do whatever you want to do with xml file
//then close it by
file.close();
File.Delete(xmlfilepath);
}
回答by Engin Ard??
Alternatively you can use this;
或者,您可以使用它;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
if (xmlDoc.FirstChild.NodeType == XmlNodeType.XmlDeclaration)
xmlDoc.RemoveChild(xmlDoc.FirstChild);
回答by Elazar Neeman
I needed to have an XML serialized string without the declaration header so the following code worked for me.
我需要一个没有声明头的 XML 序列化字符串,所以下面的代码对我有用。
StringBuilder sb = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings {
Indent = true,
OmitXmlDeclaration = true, // this makes the trick :)
IndentChars = " ",
NewLineChars = "\n",
NewLineHandling = NewLineHandling.Replace
};
using (XmlWriter writer = XmlWriter.Create(sb, settings)) {
doc.Save(writer);
}
return sb.ToString();
回答by Vladimir Zelenov
A very quick and easier solution is using the DocumentElementproperty of the XmlDocumentclass:
一个非常快速和简单的解决方案是使用类的DocumentElement属性XmlDocument:
XmlDocument doc = new XmlDocument();
doc.Load(xmlSourceFile);
Console.Out.Write(doc.DocumentElement.OuterXml);

