Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows Phone 8中的XML格式不正确_C#_Xml_Windows Phone 8 - Fatal编程技术网

C# Windows Phone 8中的XML格式不正确

C# Windows Phone 8中的XML格式不正确,c#,xml,windows-phone-8,C#,Xml,Windows Phone 8,经过几个小时的紧张工作,我终于让WP8写入xml文件……比我最初想象的要简单 不过,现在生成的文档的结构并不像预期的那样 XDocument tagRegistry = new XDocument(new XElement("SmartSafe")); if (stringUid == "" | desiredName == "" | latitude == "" | longitude == "" | stringUid == null | desiredName == nu

经过几个小时的紧张工作,我终于让WP8写入xml文件……比我最初想象的要简单

不过,现在生成的文档的结构并不像预期的那样

XDocument tagRegistry = new XDocument(new XElement("SmartSafe"));

        if (stringUid == "" | desiredName == "" | latitude == "" | longitude == "" | stringUid == null | desiredName == null | latitude == null | longitude == null)
        {
            MessageBox.Show("Something went wrong....Please try again in a moment...");
        }
        else
        {
            tagRegistry.Element("SmartSafe").Add(new XElement("Tag", 
                new XAttribute("tag", stringUid), 
                new XAttribute("name", desiredName), 
                new XAttribute("latitude", latitude), 
                new XAttribute("longitude", longitude)));
        }
        using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (Stream stream = storage.CreateFile("/tagRegistry.xml"))
            {
                tagRegistry.Save(stream);
            }
        }
这将生成以下XML文件

    <?xml version="1.0" encoding="utf-8"?>
    <SmartSafe>
      <Tag tag="2" name="Home" latitude="53.8975533333333" longitude="-1.94872666666667" />
    </SmartSafe>

我的意图是:

<?xml version="1.0" encoding="utf-8"?>
<SmartSafe>
  <Tag>
    <tag>2<tag>
    <name>Home</name>
    <latitude>12345<latitude>
    <longitude>12345<longitude/>
  </Tag>
</SmartSafe>

2.
家
12345
12345

如何更改代码以正确生成文件?

它正按照您的要求执行。您正在元素上创建属性


使用XElement而不是XAttribute

它正在做你让它做的事情。您正在元素上创建属性


使用XElement而不是XAttribute

对于这种情况,我将使用可序列化对象并从这些对象序列化为xml。对于这种情况,我将使用可序列化对象并从这些对象序列化为xml。啊!太好了,谢谢!我想这可能是我忽略了的…谢谢+1 Beeran其他快速问题…目前,xml文件被每个新项目覆盖。如何追加而不是覆盖?谢谢不要每次都创建新的空白XDocument,而是使用XDocument.Load()将现有文件读入文档,然后将新节点添加到该文档中。啊!太好了,谢谢!我想这可能是我忽略了的…谢谢+1 Beeran其他快速问题…目前,xml文件被每个新项目覆盖。如何追加而不是覆盖?谢谢不要每次都创建新的空白XDocument,而是使用XDocument.Load()将现有文件读入文档,然后将新节点添加到该文档中。