2

このようにlinqtoxmlを使用して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ルートを保存するだけです。また、新しい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 に答える