简体   繁体   English

如何删除声明并在现有XML文件(C#)中添加根元素

[英]How to remove declaration and add root element in existing XML file (C#)

I'm fairly new to XML and C#, so please understand if this question is too sily to ask. 我对XML和C#还是很陌生,所以请理解这个问题是否太愚蠢以至于无法提出。

I'm converting XML format using C# win-form application. 我正在使用C#win-form应用程序转换XML格式。 The app opens a xml file using OpenFileDialog , then conversion will be excuted(this is already done, but I still need to add or remove some more like below) . 该应用程序使用OpenFileDialog打开xml文件,然后转换将被执行(此操作已经完成,但是我仍然需要添加或删除一些类似下面的内容)。 After conversion, the app will save the modified xml file using SaveFileDialog . 转换后,应用程序将使用SaveFileDialog保存修改后的xml文件。

Original XML Format 原始XML格式

<?xml version="1.0" encoding="utf-8" ?> 
   <DataList>
    <Data>
     <ID>1</ID>
     <Name>Mike</Name>
     <Age>23</Age>
    </Data> 
    <Data>
     <ID>1</ID>
     <Name>Mike</Name>
     <Age>23</Age>
    </Data>
    <Data>
     <ID>1</ID>
     <Name>Mike</Name>
     <Age>23</Age>
    </Data>
     ..<Data></Data> continued...
   </DataList>

I want to edit the XML file as below 我想如下编辑XML文件

<?xml version="1.0" encoding="utf-8" ?> **→ Remove this delaration!**
 <MainInterface> **→ Add 'root element' before existing nodes**
   <DataList>
    <Data>
     <ID>1</ID>
     <Name>Mike</Name>
     <Age>23</Age>
    </Data> 
    <Data>
     <ID>1</ID>
     <Name>Mike</Name>
     <Age>23</Age>
    </Data>
    <Data>
     <ID>1</ID>
     <Name>Mike</Name>
     <Age>23</Age>
    </Data>
     ..<Data></Data> continued...
   </DataList>
 </MainInterface> **→ close newly added root element**

I've tried below code but seem like it doesn't work 我试过下面的代码,但似乎不起作用

OpenFileDialog openFileDialogue = new OpenFileDialog();           
openFileDialog1.DefaultExt = "xml";
openFileDialog1.Filter = "xml files (*.xml)|*.xml";
openFileDialog1.Title = "Select a xml File";
openFileDialog1.ShowDialog();

XDocument xmlFile = XDocument.Load(openFileDialog1.FileName);
**// Remove Declaration**
XDocument doc = new XDocument(new XDeclaration(null, null, null));

**// Add Root Element**
XElement doc1 = XElement.Parse(openFileDialog1.FileName);
XElement root = new XElement("MainInterface", doc1);
//doc.Save(_data)
openFileDialog1.FileName = root.ToString();

-----------------------------------------------------------------------------------
Do something for conversion ~~~
-----------------------------------------------------------------------------------
SaveFileDialog saveFileDialogue1 = new SaveFileDialog();
saveFileDialog1.Filter = "xml File |*.xml";
saveFileDialog1.Title = "Conversion Completed! Save a XML file";
saveFileDialog1.FileName = "XML Converted.xml";            
saveFileDialog1.ShowDialog();
xmlFile.Save(saveFileDialog1.FileName);

The point is I'm not creating new XML file, but modifying existing xml file to get rid of declaration and add a root element. 关键是我不是在创建新的XML文件,而是在修改现有的xml文件以摆脱声明并添加根元素。 Should I use XML Writer? 我应该使用XML Writer吗? Is there simpler way of doing these? 有更简单的方法可以做到这些吗? Thank you in advance. 先感谢您。

Thank you for your answers. 谢谢您的回答。 I found out this works for me! 我发现这对我有用!

SaveFileDialog saveFileDialogue1 = new SaveFileDialog();
saveFileDialog1.Filter = "xml File |*.xml";
saveFileDialog1.Title = "Conversion Completed! Save a XML file";
saveFileDialog1.FileName = "XML Converted.xml";            
saveFileDialog1.ShowDialog();

XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
StringWriter sw = new StringWriter();
using (XmlWriter xw = XmlWriter.Create(saveFileDialog1.FileName, settings))
   {
      xmlFile.Save(xw);
   }
string s = sw.ToString();

I would create a new XDocument , but just save it over the top of the old one: 我将创建一个新的 XDocument ,但只需将其保存在旧文件的顶部即可:

// You don't want XElement.Parse here - that treats the filename as the
// XML itself!
XDocument oldDocument = XDocument.Load(openFileDialog1.FileName);
XDocument newDocument = new XDocument(new XElement("MainInterface", 
                                                   oldDocument.Root));
newDocument.Save(saveFileDialog1.FileName);

Create a new XmlDocument with only your root, then add the opened document to the first one excluding the Declaration: 仅使用您的根创建一个新的XmlDocument ,然后将打开的文档添加到第一个不包含“声明”的文档中:

foreach(XmlNode node in doc)
    if (node.NodeType != XmlNodeType.XmlDeclaration)
    {
// Add node logic here
    }

Write (or overwrite) with XmlWriter. 用XmlWriter编写(或覆盖)。

XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM