• <small id='ulMQY'></small><noframes id='ulMQY'>

    <i id='ulMQY'><tr id='ulMQY'><dt id='ulMQY'><q id='ulMQY'><span id='ulMQY'><b id='ulMQY'><form id='ulMQY'><ins id='ulMQY'></ins><ul id='ulMQY'></ul><sub id='ulMQY'></sub></form><legend id='ulMQY'></legend><bdo id='ulMQY'><pre id='ulMQY'><center id='ulMQY'></center></pre></bdo></b><th id='ulMQY'></th></span></q></dt></tr></i><div id='ulMQY'><tfoot id='ulMQY'></tfoot><dl id='ulMQY'><fieldset id='ulMQY'></fieldset></dl></div>
  • <tfoot id='ulMQY'></tfoot>

      • <bdo id='ulMQY'></bdo><ul id='ulMQY'></ul>

        <legend id='ulMQY'><style id='ulMQY'><dir id='ulMQY'><q id='ulMQY'></q></dir></style></legend>
      1. 您如何强制使用 Linq XML 关闭显式标记?

        时间:2023-11-10
          <tbody id='f5gPG'></tbody>

        • <small id='f5gPG'></small><noframes id='f5gPG'>

          <tfoot id='f5gPG'></tfoot>
          1. <legend id='f5gPG'><style id='f5gPG'><dir id='f5gPG'><q id='f5gPG'></q></dir></style></legend>

              • <i id='f5gPG'><tr id='f5gPG'><dt id='f5gPG'><q id='f5gPG'><span id='f5gPG'><b id='f5gPG'><form id='f5gPG'><ins id='f5gPG'></ins><ul id='f5gPG'></ul><sub id='f5gPG'></sub></form><legend id='f5gPG'></legend><bdo id='f5gPG'><pre id='f5gPG'><center id='f5gPG'></center></pre></bdo></b><th id='f5gPG'></th></span></q></dt></tr></i><div id='f5gPG'><tfoot id='f5gPG'></tfoot><dl id='f5gPG'><fieldset id='f5gPG'></fieldset></dl></div>
                  <bdo id='f5gPG'></bdo><ul id='f5gPG'></ul>
                  本文介绍了您如何强制使用 Linq XML 关闭显式标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这与以下问题相同:使用 System.Xml.Linq 命名空间的显式元素关闭标记

                  但我使用 Net 4.0 并且答案不再有效.

                  but I use Net 4.0 and the answers do not work anymore.

                  问题是我保存的标签实际上没有值,我的输出 XML 看起来像这样:

                  The problem is I save tags with no values really, and my output XML looks like this:

                  <field/>

                  但我需要的始终是开始和结束标记,即

                  But what I need is always opening and closing tag, i.e.

                  <field></field>

                  问题:怎么做?

                  添加空节点:

                  if (field_xml == null) // always true, because I create the file for the first time
                  {
                      field_xml = new XElement(XMLKeys.field,String.Empty);
                      table_xml.Add(field_xml);
                  }
                  field_xml.SetAttributeValue(XMLKeys.name, field_info.Name);
                  // ... setting some other attributes of this node
                  

                  之后,保存xml:

                  var writer = new FullEndingXmlTextWriter(parameters.OutputFilename, Encoding.UTF8);
                  root_xml.Save(writer);
                  

                  FullEndingXmlTextWriter 是 The Evil Greebo 指出的专用类(它应该强制显式结束标记).

                  FullEndingXmlTextWriter is the specialized class which The Evil Greebo pointed out (it is supposed to force explicit closing tag).

                  推荐答案

                  我无法重现您的错误.这在 4.0 和 3.5 netFX 中都按预期工作:

                  I can't reproduce your error. This works as expected in both 4.0 and 3.5 netFX:

                  namespace ExplicitXmlClosingTags
                  {
                      using System.Xml;
                      using System.Xml.Linq;
                  
                      class Program
                      {
                          static void Main(string[] args)
                          {
                              const string ElementRoot = "RootElement";
                              const string ElementChild = "ChildElement";
                              const string AttributeChild = "ChildAttribute";
                  
                              XDocument xDoc = new XDocument();
                              XElement root = new XElement(ElementRoot);
                              XElement child = new XElement(ElementChild, string.Empty);
                              root.Add(child);
                  
                              child.SetAttributeValue(AttributeChild, "AttrValue");
                              xDoc.Add(root);
                  
                              XmlWriterSettings xws = new XmlWriterSettings();
                              xws.Indent = true;
                              using (XmlWriter xw = XmlWriter.Create("out.xml", xws))
                              {
                                  xDoc.Save(xw);    
                              }
                          }
                      }
                  }
                  

                  产生以下内容:

                  <?xml version="1.0" encoding="utf-8"?>
                  <RootElement>
                    <ChildElement ChildAttribute="AttrValue"></ChildElement>
                  </RootElement>
                  

                  这篇关于您如何强制使用 Linq XML 关闭显式标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 LINQ 过滤列表 下一篇:C# - 添加、编辑和删除文件的标签

                  相关文章

                    <bdo id='Jdr16'></bdo><ul id='Jdr16'></ul>

                      <tfoot id='Jdr16'></tfoot>
                      <i id='Jdr16'><tr id='Jdr16'><dt id='Jdr16'><q id='Jdr16'><span id='Jdr16'><b id='Jdr16'><form id='Jdr16'><ins id='Jdr16'></ins><ul id='Jdr16'></ul><sub id='Jdr16'></sub></form><legend id='Jdr16'></legend><bdo id='Jdr16'><pre id='Jdr16'><center id='Jdr16'></center></pre></bdo></b><th id='Jdr16'></th></span></q></dt></tr></i><div id='Jdr16'><tfoot id='Jdr16'></tfoot><dl id='Jdr16'><fieldset id='Jdr16'></fieldset></dl></div>

                      <small id='Jdr16'></small><noframes id='Jdr16'>

                    1. <legend id='Jdr16'><style id='Jdr16'><dir id='Jdr16'><q id='Jdr16'></q></dir></style></legend>