2

我想像这样使用 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);
                }
            }
        }
4

2 回答 2

3

您可以简单地保存XElement哪个是root。当您创建新的 xml 文件时,您不需要加载任何内容:

public void CreatXmlConfigurationFileIfNotFoundWithDefultTags(string path)
{
    if (!File.Exists(path))
    {
        try
        {
            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"))
              );

            var config = new XElement("Configuration", setting,
                new XElement("IncomingCallsConfiguration"),
                new XElement("OutGoingCallsConfiguration")));

            config.Save(path); // save XElement to file
        }
        catch (Exception e)
        {
            Trace.WriteLineIf(Logger.logSwitch.TraceError, e.Message);
        }
    }
}

如果您想使用 XDocument (在您的情况下不需要),那么只需创建新的 XDocument 而不是加载不存在的文件:

XDocument document = new XDocument();
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);
于 2013-01-23T14:59:41.333 回答
3

好吧,您尝试使用 XDocument.Load() “读取/解析”一个新的空文档

File.Create(path).Close();
XDocument document = XDocument.Load(path);

并且XDocument.Load()想要一个正确的xml文件......他没有(文件为空)!

所以你可以做

var document =  new XDocument();
//...
document.Save(path);
于 2013-01-23T15:01:36.063 回答