根元素缺失。例外

时间:2013-01-23 14:56:54

标签: c# linq-to-xml

我想使用linq创建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);
                }
            }
        }

2 个答案:

答案 0 :(得分:3)

您只需保存root XElement即可。在创建新的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);

答案 1 :(得分:3)

好吧,你试图用XDocument.Load()

“读/解”一个新的空文档
File.Create(path).Close();
XDocument document = XDocument.Load(path);

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

所以你可以做到

var document =  new XDocument();
//...
document.Save(path);