1

我对 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 文件中删除声明和添加根元素?先感谢您。

4

2 回答 2

0

那是你的 XML 结构吗?还是一定会改变?

请参阅我的解析方式:

var xDoc    = XDocument.Load(openFileDialog1.FileName);
//Use code below if you'll use string to Load XDocument
/*var xmlString = @"<?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>
";

var xDoc     = XDocument.Parse(xmlString);*/

var dataList = xDoc.Descendants(@"Data");   
var newXDoc  = new XDocument(new XDeclaration(null, null, null),
                   new XElement("MainInterface",
                       new XElement("DataList",
                           dataList.Select(data =>
                               new XElement("Data",
                                   data.Element("ID"),
                                   data.Element("Name"),
                                   data.Element("Age")
                               )
                           ) 
                       )                              
                   )
               );

请参阅此图片链接,了解我的带有 LINQPad 的 XML 转储。

于 2013-04-29T05:41:53.187 回答
-1

Just get xml as string and play with it! To remove header you can use Replace function! To add root element just add <MainInterface> to the begining and </MainInterface> to the end of xml string.

To convert to string you can use XDocument.ToString()

于 2013-04-29T04:52:35.353 回答