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

标签 c# xml declaration

我对 XML 和 C# 还很陌生,所以请理解这个问题是否太愚蠢而无法提出。

我正在使用 C# win-form 应用程序转换 XML 格式。该应用程序使用OpenFileDialog打开一个xml文件,然后将执行转换(这已经完成,但我仍然需要添加或删除更多内容,如下所示)。转换后,应用程序将使用 SaveFileDialog 保存修改后的 xml 文件。

原始 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>

我想编辑如下 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**

我尝试了下面的代码,但似乎不起作用

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);

重点是我不是创建新的 XML 文件,而是修改现有的 xml 文件以删除声明并添加根元素。我应该使用 XML Writer 吗?有没有更简单的方法来做到这些?预先感谢您。

感谢您的回答。我发现这对我有用!

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();

最佳答案

我会创建一个 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);

关于c# - 如何在现有 XML 文件中删除声明并添加根元素 (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16278880/

相关文章:

c# - 以编程方式激活 Outlook

c - C 中 long int 和 int 的大小显示 4 个字节

python - 使用 Python Elementtree 访问 XMLNS 属性?

C Puzzle - 玩类型

java - 为什么 Java 允许类型不安全的数组赋值?

c# - 在 Razor View 中有条件地更改 CSS 类

c# - Windows.Forms SplitContainer.SplitterWidth 不会在运行时保持设置

c# - 如何使用 join 加速 LINQ 查询?

javascript - 通过 Javascript 检索跨域 RSS(xml)

java - 如何使用请求路径在 Mule ESB 中进行路由