【问题标题】:How to save the value of the string variable to an .xml file?如何将字符串变量的值保存到 .xml 文件中?
【发布时间】:2019-08-13 06:12:51
【问题描述】:

我无法使用 XElement 将字符串变量的值写入 .xml 文件。

我尝试使用System.IOXDocumentXElement

code.cs:

string variable ="sth";
XDocument xml_cip_c1 = new XDocument(
    new XComment("document"),
    new XElement("new root"),
    new XElement("name", variable)
);

result.xml:

<!--document-->
<new root>
    <name />
</new root>

【问题讨论】:

  • 您在寻找&lt;name value = "ABC" /&gt;吗?
  • 我不知道如何在这里编辑帖子 // 变量字符串 variable ="sth";
  • 点击帖子下方的edit按钮
  • @MichałParzych 如果以下答案之一解决了您的问题,请记住将其标记为已接受。

标签: c# xml wpf xaml


【解决方案1】:

你是先生。使用Value属性:

var yourVariable = "ABC";
XDocument xml_cip_c1 = new XDocument(
    new XComment("document"),
    new XElement("new_root",
    new XElement("name") { Value = yourVariable}));

这将产生以下输出:

<?xml version="1.0" encoding="utf-8"?>
<!--document-->
<new_root>
  <name>ABC</name>
</new_root>

但是,如果您想将属性添加到您的 xml 元素,请使用以下使用 XAttribute 的代码:

XDocument xml_cip_c1 = new XDocument(
    new XComment("document"),
    new XElement("new_root",
    new XElement("name", new XAttribute("name", yourVariable))));

然后你会得到如下的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<!--document-->
<new_root>
  <name name="ASD" />
</new_root>

【讨论】:

    【解决方案2】:

    试试这个,

    .cs 代码

    XmlWriterSettings settings = new XmlWriterSettings();  
    settings.Indent =true;  
    settings.IndentChars = ("    ");  
    settings.CloseOutput = true;  
    settings.OmitXmlDeclaration =true;                  
    using (XmlWriter writer = XmlWriter.Create("FileName.xml", settings))  
    {  
    
        writer.WriteStartElement("newroot");  
        writer.WriteElementString("name", "ABC");  
        writer.WriteEndElement();  
        writer.Flush();  
    }
    

    结果 => XML 文件

    <?xml version="1.0" encoding="utf-8" ?>
    <newroot>
       <name>ABC</name>
    </newroot>
    

    【讨论】:

      【解决方案3】:

      我使用第一篇文章的方法,

      代码.cs:

        private void sb_button_click(object sender, RoutedEventArgs e)
              {
               variable1 = przycisk.Text;
               name_of_file_xml.Save(path_of_file);
              }
      
              static string variable1 ="sth"; 
      

      我必须使用静态变量,因为它需要我的 XAttribute。

      我必须使用带值的字符串,因为如果我声明字符串不带值:

      静态字符串 zmienna1 = null;

      System.ArgumentNullException: '值不能为零。

      代码.cs:

      Parameter name: value '
      XDocument name_of_file_xml = new XDocument(
                  new XComment("document"),
                  new XElement("root",
                      new XElement("name", new XAttribute ("name2", variable1 ))
                      )
                  );
      

      在我的项目中我想要一个没有值的变量,这可能吗?

      因为它没有将 button.text 中的变量分配给我,而是在开头设置的那个

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-05
        • 2013-04-30
        • 1970-01-01
        • 2021-03-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多