2

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

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

原始 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 编写器吗?有没有更简单的方法来做这些?先感谢您。

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

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

2 回答 2

4

我会创建一个 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);
于 2013-04-29T12:44:02.350 回答
0

仅使用您的根创建一个新XmlDocument的,然后将打开的文档添加到第一个不包括声明的文档:

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

使用 XmlWriter 写入(或覆盖)。

XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;
于 2013-04-29T13:01:03.990 回答