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

时间:2013-04-29 12:40:30

标签: 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();

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分: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;