C#WPF无法写入XML文件

时间:2018-05-04 21:53:52

标签: c# xml xmldocument

我是WPF的新手,我正在尝试将子节点写入XML文件。这是文件......

<?xml version="1.0" encoding="utf-8"?>
<Sequences>
  <LastSavedSequence name="Last Saved Sequence">    
     <Test name="Measure Battery Current(Stim)" testNumber="5" Vbat="3.7" Frequency="20" PulseWidth="500" Amplitude="1" Resistance="1600" Anode="1" Cathode="2" ActiveDischarge=""/>
     <Test name="Measure Batther Current(No Stim)" testNumber="6" Vbat="2.9" Frequency="20" PulseWidth="500" Amplitude="1" Resistance="1600" Anode="1" Cathode="2" ActiveDischarge=""/>
  </LastSavedSequence>  
  <ScottTestSequence name="Scott Test Sequence">    
     <Test name="VMO Status" testNumber="4" Vbat="3.7" Frequency="20" PulseWidth="1000" Amplitude="6" Resistance="3000" Anode="1" Cathode="2" ActiveDischarge=""/>
     <Test name="Measure Battery Current(Stim)" testNumber="5" Vbat="3.7" Frequency="20" PulseWidth="500" Amplitude="1" Resistance="1600" Anode="1" Cathode="2" ActiveDischarge=""/>
     <Test name="Measure Batther Current(No Stim)" testNumber="6" Vbat="2.9" Frequency="20" PulseWidth="500" Amplitude="1" Resistance="1600" Anode="1" Cathode="2" ActiveDischarge=""/>
  </ScottTestSequence>  
</Sequences>

我正在尝试创建一个XML子块以进入。我使用了stringBuilder然后尝试做一个附加子项,然后是.save。 XMLData2是一个全局列表,包含我在每个元素中获得的子元素。这是我的代码......

public static List<System.Xml.XmlNode> xmlData2 = new List<System.Xml.XmlNode>();
XmlDocument xmlFromOutSideSequenceFile = new XmlDocument();
                    xmlFromOutSideSequenceFile.Load("c:\\Users/StarkS02/Documents/SavedSequenceFile.xml");
StringBuilder exampleNode = new StringBuilder();
                        exampleNode.Append("<");
                        exampleNode.Append(tbSequenceName.Text.ToString().Replace(" ", ""));
                        exampleNode.Append(" name=");
                        exampleNode.Append("'");
                        exampleNode.Append(tbSequenceName.Text);
                        exampleNode.Append("'");
                        exampleNode.Append(">");
foreach (XmlNode node in xmlData2)
                        {
XmlElement child = xmlFromOutSideSequenceFile.CreateElement(string.Empty, node.OuterXml, string.Empty);
exampleNode.Append("</");
                        exampleNode.Append(tbSequenceName.Text.ToString().Replace(" ", ""));
                        exampleNode.Append(">");
xmlFromOutSideSequenceFile.AppendChild(exampleNode);
xmlFromOutSideSequenceFile.Save("c:\\Users/StarkS02/Documents/SavedSequenceFile.xml");

我在.appendChild语句中遇到编译器错误,我无法将stringBuilder转换为XML节点。这是有道理的,但我不知道如何解决它。有任何想法吗?

1 个答案:

答案 0 :(得分:2)

您可以创建XML片段并附加到文档。

var xmlFromOutSideSequenceFile = new XmlDocument();
xmlFromOutSideSequenceFile.Load("c:\\Users/StarkS02/Documents/SavedSequenceFile.xml");

有关DocumentFragment的更多信息,请参阅此处

https://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createdocumentfragment(v=vs.110).aspx

var fragment = xmlFromOutSideSequenceFile.CreateDocumentFragment();
fragment.InnerXml = @"<somexml></somexml>";
xmlFromOutSideSequenceFile.DocumentElement.FirstChild.AppendChild(fragment);

有关XMLNode的更多信息,请参阅此处

https://msdn.microsoft.com/en-us/library/system.xml.xmlnode(v=vs.110).aspx

决定你想把它放在哪里。     。第一个孩子     。最后一个孩子     .NextSibling     .ParentNode     .PreviousSibling

希望这有帮助!干杯!