簡體   English   中英

根元素丟失。 例外

[英]Root element is missing. exception

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

您可以簡單地保存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);

好吧,您嘗試使用XDocument.Load()“讀取/解析”一個新的空文檔。

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

XDocument.Load()一個正確的xml文件...他沒有(文件為空)!

所以你可以做

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM