我正在尝试读取一个 xml 字符串,然后通过插入另一个 xml 字符串来修改它,我不知道该怎么做,因为我是这个领域的初学者,所以我很挣扎。我在网上搜索,我真的很困惑 xmlWriter 和 xmlReader 因为我不想使用 xmlDocument 我从微软 http://msdn.microsoft.com/en-us/library/cc189056(v=vs.95) 找到了这个。 aspx
所以我的代码是这样的
String Mainxmltext = "<ClosedKPICalls>" +
"<ServiceInfo> <ServiceNo>HR011</ServiceNo> <ServiceName>Petty cash</ServiceName> "+
"<RecCount>0</RecCount> <RegdKPI>2</RegdKPI> </ServiceInfo> </ClosedKPICalls>";
String xmlinnerText = "<TaskDetails> <name>Jhone</name> <Description>just for testing</Description> </TaskDetails>";
StringBuilder output = new StringBuilder();
using (XmlReader reader = XmlReader.Create(new StringReader(Mainxmltext)))
{
XmlWriterSettings ws = new XmlWriterSettings();
ws.Indent = true;
using (XmlWriter writer = XmlWriter.Create(output, ws))
{
// Parse the file and display each of the nodes.
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
writer.WriteStartElement(reader.Name);
break;
case XmlNodeType.Text:
writer.WriteString(reader.Value);
break;
case XmlNodeType.XmlDeclaration:
case XmlNodeType.ProcessingInstruction:
writer.WriteProcessingInstruction(reader.Name, reader.Value);
break;
case XmlNodeType.Comment:
writer.WriteComment(reader.Value);
break;
case XmlNodeType.EndElement:
writer.WriteFullEndElement();
break;
}
}
}
}
MessageBox.Show(output.ToString());
}
所以我现在成功读取了第一个字符串我如何在我之前的意思的主 xml 的结束标记之前插入第二个测试,我的 xml 应该看起来像这样
<ClosedKPICalls>
<ServiceInfo>
<ServiceNo>HR011</ServiceNo>
<ServiceName>Petty cash</ServiceName>
<RecCount>0</RecCount>
<RegdKPI>2</RegdKPI>
</ServiceInfo>
<TaskDetails>
<name>Jhone</name>
<Description>just for testing</Description>
</TaskDetails>
</ClosedKPICalls>
请帮忙谢谢