生成
XmlDocument document = new XmlDocument();//创建XmlDocument对象
XmlDeclaration declaration = document.CreateXmlDeclaration("1.0", "UTF-8", "");//xml文档的声明部分
document.AppendChild(declaration);
XmlElement root = document.CreateElement("Nanox", "FtpFileXml", "nanox@nanox-medical.com");
document.AppendChild(root);
XmlElement Bondage = document.CreateElement("Bondage");
Bondage.InnerText = "10";
root.AppendChild(Bondage);
string binFile = RunningPath + @"../../Images/test.xml";
document.Save(binFile);
读取
string binFile = RunningPath + @"../../Images/test.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(binFile);
XmlNodeList nodes = xmlDoc.DocumentElement.ChildNodes;
foreach (XmlNode node in nodes)
{
Console.WriteLine(node.Name + ": " + node.InnerText);
}
修改
XmlDocument doc = new XmlDocument();
doc.Load("example.xml");
XmlNodeList nodes = doc.DocumentElement.ChildNodes;
foreach (XmlNode node in nodes)
{
if (node.Name == "book")
{
XmlAttribute attr = doc.CreateAttribute("genre");
attr.Value = "fantasy";
node.Attributes.Append(attr);
}
}
doc.Save("example.xml");
var inputStream = File.Open(myDecryptedFile, FileMode.Open);
var reader = new BinaryReader(inputStream);
string s = reader.ReadString();
UseNumberCount = Convert.ToInt16(System.Text.RegularExpressions.Regex.Replace(s, @"[^0-9]+", ""));
reader.Close();
string binFile = directoryFile + @"/config.bin";
var outputStream = File.Create(binFile);
using (var writer = new BinaryWriter(outputStream))
{
writer.Write("bondage count :" + UseNumberCount + "\r\n");
writer.Close();
}