我想像这样使用 linq to xml 创建 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<Settings>
<UseStreemCodec value="false" />
<SipPort value="5060"/>
<H323Port value="1720" />
</Settings>
<IncomingCallsConfiguration>
</IncomingCallsConfiguration>
<OutGoingCallsConfiguration>
<Devices>
</Devices>
</OutGoingCallsConfiguration>
</Configuration>
我试试这段代码,但给我一个Root element is missing.
例外
public void CreatXmlConfigurationFileIfNotFoundWithDefultTags(string path)
{
if (!File.Exists(path))
{
try
{
File.Create(path).Close();
XDocument document = XDocument.Load(path);
var setting = new XElement("Settings",
new XElement("UseStreemCodec", new XAttribute("value", "false")),
new XElement("SipPort", new XAttribute("value", "5060")),
new XElement("H323Port", new XAttribute("value", "1720"))
);
document.Add(new XElement("Configuration", setting,
new XElement("IncomingCallsConfiguration"),
new XElement("OutGoingCallsConfiguration")));
document.Save(path);
}
catch (Exception e)
{
Trace.WriteLineIf(Logger.logSwitch.TraceError, e.Message);
}
}
}