1

在我的项目中,我加载了一个 XML 文件并通过填充内部文本来填充其标签。我没有填充所有节点并留下一些空值。

在我保存 XML 文件后,那些设置为 null 的子节点内部文本被写为 5 个空格?

如何将节点的值设置为空?

另一种方法是如何从我的 XML 文件中删除子节点?

这是我填充节点内部文本的方式,我能做的最好的事情是 1 个空格,但我希望没有。


XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(MapPath(tempFolderPathAlt + "dvforms" + "\\XmlDataTemplate.xml"));

XmlNodeList dataNodes = xmlDoc.SelectNodes("//FieldData");
foreach (XmlNode node in dataNodes)
   {
      int Count = 0;
      foreach (XmlNode childNode in node.ChildNodes) //For each child node in FieldData
        {
           if (PropInfo[Count].TagName == childNode.Name)
              {
                if (PropInfo[Count].TagValue != null)
                   {
                    node.SelectSingleNode(PropInfo[Count].TagName).InnerText = PropInfo[Count].TagValue;
                   }
                   else
                   {
                     //node.RemoveChild(childNode);
                     //xmlDoc.
                     // xmlDoc.RemoveChild(childNode);
                     //node.RemoveChild(PropInfo[Count].TagName);
                     node.SelectSingleNode(PropInfo[Count].TagName).InnerText = " ";
                   }
                     PropInfo[Count].TagMod = childNode.Attributes.Item(0).InnerText;
                     PropInfo[Count].TagIncludeInPdf = childNode.Attributes.Item(1).InnerText;
               }
               Count = Count + 1;
         }
    }

目前我已将所有空值设置为一个空格,我希望它们为空值还是删除它们?


我的 XML 文件的外观

<?xml version="1.0"?>
<FormData Platform="Android" PlatformVersion="89" Version="360" DataVersion="1" Description="New Investec - Residential" FormId="9fe48133-ebec-415c-8581-22ba1bfcb237" FileId="d204b364-6b3f-46e0-beed-3f3b2783b8f3" EncryptionVerification="" CreatedBy="Johanc" EditedBy="Johanc" TrackingNum="FormMobi19e9ba3a-2c70-4a5a-a881-44cba4a031c5">
  <FieldData>
    <request_details_lightstone_report Mod="20010101010101" IncludeInPDFExport="Yes"> </request_details_lightstone_report>
    <request_details_export_template Mod="20010101010101" IncludeInPDFExport="Yes"> </request_details_export_template>
    <request_details_reason_for_valuatio Mod="20010101010101" IncludeInPDFExport="Yes"> </request_details_reason_for_valuatio>
    <request_details_valuation_job_numbe Mod="20010101010101" IncludeInPDFExport="Yes"> </request_details_valuation_job_numbe>

    <property_details_gps_location_lat Mod="20010101010101" IncludeInPDFExport="Yes">-29.965495</property_details_gps_location_lat>
    <property_details_gps_location_long Mod="20010101010101" IncludeInPDFExport="Yes">30.838881</property_details_gps_location_long>
    <property_details_address_address1 Mod="20010101010101" IncludeInPDFExport="Yes">8 LETHUBUHLE</property_details_address_address1>
    <property_details_address_address2 Mod="20010101010101" IncludeInPDFExport="Yes">UMLAZI SOUTH</property_details_address_address2>
    <property_details_address_city Mod="20010101010101" IncludeInPDFExport="Yes">ETHEKWINI</property_details_address_city>
    <property_details_address_state Mod="20010101010101" IncludeInPDFExport="Yes">KWAZULU NATAL</property_details_address_state>
    <property_details_address_zip Mod="20010101010101" IncludeInPDFExport="Yes"> </property_details_address_zip>
    <property_details_locality_map Mod="20010101010101" IncludeInPDFExport="Yes">a8267608-6693-40d4-a3e6-d46dfd888037.jpg</property_details_locality_map>
    <property_details_type_of_ownership Mod="20010101010101" IncludeInPDFExport="Yes">Freehold</property_details_type_of_ownership>
    <property_details_text_box Mod="20010101010101" IncludeInPDFExport="Yes"> </property_details_text_box>
    <property_details_text_box_1 Mod="20010101010101" IncludeInPDFExport="Yes"> </property_details_text_box_1>
    <property_details_area_of_site Mod="20010101010101" IncludeInPDFExport="Yes">840</property_details_area_of_site>
    <field_group Mod="20010101010101" IncludeInPDFExport="Yes"> </field_group>
    <property_description_erf_number Mod="20010101010101" IncludeInPDFExport="Yes">532</property_description_erf_number>
    <property_description_portion_number Mod="20010101010101" IncludeInPDFExport="Yes">0</property_description_portion_number>
    <Property_description_township_name Mod="20010101010101" IncludeInPDFExport="Yes">UMLAZI DD</Property_description_township_name>
    <property_description_sectional_titl Mod="20010101010101" IncludeInPDFExport="Yes">
    </property_description_sectional_titl>
    <property_description_section_number Mod="20010101010101" IncludeInPDFExport="Yes"></property_description_section_number>
    <title_deed__municipal_information_text_box Mod="20010101010101" IncludeInPDFExport="Yes"> </title_deed__municipal_information_text_box>
    <title_deed__municipal_information_text_box_1 Mod="20010101010101" IncludeInPDFExport="Yes">, ETHEKWINI MUNICIPALITY</title_deed__municipal_information_text_box_1>
    <title_deed__municipal_information_text_box_2 Mod="20010101010101" IncludeInPDFExport="Yes"> </title_deed__municipal_information_text_box_2>
    <title_deed__municipal_information__2 Mod="20010101010101" IncludeInPDFExport="Yes"> </title_deed__municipal_information__2>
    <title_deed__municipal_information_text_box_3 Mod="20010101010101" IncludeInPDFExport="Yes">
    </title_deed__municipal_information_text_box_3>
    <title_deed__municipal_information_text_area Mod="20010101010101" IncludeInPDFExport="Yes"> </title_deed__municipal_information_text_area>
    <title_deed__municipal_information__1 Mod="20010101010101" IncludeInPDFExport="Yes"> </title_deed__municipal_information__1>
    <title_deed__municipal_information_text_area_1 Mod="20010101010101" IncludeInPDFExport="Yes"> </title_deed__municipal_information_text_area_1>
    <title_deed__municipal_information_text_box_4 Mod="20010101010101" IncludeInPDFExport="Yes"> </title_deed__municipal_information_text_box_4>
    <title_deed__municipal_information_text_area_2 Mod="20010101010101" IncludeInPDFExport="Yes"> </title_deed__municipal_information_text_area_2>
    <main_dwelling_text_box Mod="20010101010101" IncludeInPDFExport="Yes"> </main_dwelling_text_box>
    <main_dwelling_text_box_1 Mod="20010101010101" IncludeInPDFExport="Yes"> </main_dwelling_text_box_1>
    <main_dwelling_text_box_2 Mod="20010101010101" IncludeInPDFExport="Yes"> </main_dwelling_text_box_2>
    <main_dwelling_text_box_3 Mod="20010101010101" IncludeInPDFExport="Yes"> </main_dwelling_text_box_3>
    <valuation_certificate_signature_1 Mod="20010101010101" IncludeInPDFExport="Yes"> </valuation_certificate_signature_1>
    <valuation_certificate_professional_ Mod="20010101010101" IncludeInPDFExport="Yes"> </valuation_certificate_professional_>
  </FieldData>
  <Photos Mod="20010101010101" />
  <VoiceNotes />
  <Drawings Mod="20010101010101" />
  <FieldNotes />
</FormData>
4

3 回答 3

0

如果值为 NULL,请尝试将内部文本设置为 string.empty。这只是一个猜测

于 2012-11-22T12:13:48.760 回答
0

干脆不写这一行

node.SelectSingleNode(PropInfo[Count].TagName).InnerText = " ";

这不会设置值,但节点会在那里。

或者

node.SelectSingleNode(PropInfo[Count].TagName).InnerText = String.Empty;

那应该行得通。

米林德

于 2012-11-22T12:21:43.547 回答
0

node.RemoveAll(); 如果“节点”中包含的唯一内容是 InnerText,则将去掉多余的空格。

于 2013-05-06T22:23:04.923 回答