简体   繁体   English

根元素丢失。 例外

[英]Root element is missing. exception

i want to create xml file using linq to xml like this 我想使用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>

i try this code but give me an Root element is missing. 我尝试此代码,但给我一个Root element is missing. exception 例外

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);
                }
            }
        }

You can simply save XElement which is root. 您可以简单地保存root的XElement And you don't need to load anything when you are creating new xml file: 创建新的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);
        }
    }
}

If you want to use XDocument (which is not needed in your case), then just create new XDocument instead of loading non-existing file: 如果要使用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);

Well, you try to "read/parse" a new empty document with XDocument.Load() 好吧,您尝试使用XDocument.Load()“读取/解析”一个新的空文档。

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

and XDocument.Load() wants a correct xml file... which he has not (file is empty) ! XDocument.Load()一个正确的xml文件...他没有(文件为空)!

So you could just do 所以你可以做

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM