c# Linq XML - 为什么要将空格添加到带有引号/命名空间的元素中?

c# Linq XML - Why Is Whitespace Being Added to Elements With Quotes / Namespace?

我的 c# .NET 程序的一部分包含修改 XML 文档中的元素的代码。根据我在代码其他地方设置的变量修改值,代码工作正常,但问题是当 xml 文件保存时,空格被添加到所有元素更新。

我根本没有在我的代码中访问这个元素。我假设这是因为算法命名空间值的引号,因为我看不出会发生这种情况的任何其他原因。我在加载时使用 Preserve Namespace,在保存时使用 Disable Formatting。

所以问题是,为什么要添加这个额外的空格,我该如何阻止它?

XML(源文件)

<?xml version="1.0" encoding="UTF-8"?>
<PackingList xmlns="http://www.smpte-ra.org/schemas/2067-2/2016/PKL">
<Id>urn:uuid:296a656c-3610-4de1-9b08-2aa63245788d</Id>
<AnnotationText>JOT_Sample</AnnotationText>
<IssueDate>2018-02-16T20:59:42-00:00</IssueDate>
<Issuer>Generic</Issuer>
<Creator>Generic</Creator>
<AssetList>
  <Asset>
    <Id>urn:uuid:744f36b7-fc7e-4179-8b75-c71c18f98156</Id>
    <AnnotationText>Video_744f36b7-fc7e-4179-8b75-c71c18f98156.mxf</AnnotationText>
    <Hash>8HhnKnLn+Lp/Ik9i94Ml4SXAxH4=</Hash>
    <Size>14568486</Size>
    <Type>application/mxf</Type>
    <OriginalFileName>Video_744f36b7-fc7e-4179-8b75-c71c18f98156.mxf</OriginalFileName>
    <HashAlgorithm Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
  </Asset>
  <Asset>
    <Id>urn:uuid:bf5438ea-ba58-4ae0-a64a-5d23cee2ebb3</Id>
    <AnnotationText>Audio_bf5438ea-ba58-4ae0-a64a-5d23cee2ebb3.mxf</AnnotationText>
    <Hash>Wg4aEAE5Ji9e14ZyGkvfUUjBwCw=</Hash>
    <Size>4341294</Size>
    <Type>application/mxf</Type>
    <OriginalFileName>Audio_bf5438ea-ba58-4ae0-a64a-5d23cee2ebb3.mxf</OriginalFileName>
    <HashAlgorithm Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
  </Asset>
</AssetList>
</PackingList>

XML(输出文件)

<?xml version="1.0" encoding="UTF-8"?>
<PackingList xmlns="http://www.smpte-ra.org/schemas/2067-2/2016/PKL">
<Id>urn:uuid:296a656c-3610-4de1-9b08-2aa63245788d</Id>
<AnnotationText>JOT_Sample</AnnotationText>
<IssueDate>2018-02-16T20:59:42-00:00</IssueDate>
<Issuer>Generic</Issuer>
<Creator>Generic</Creator>
<AssetList>
  <Asset>
    <Id>urn:uuid:744f36b7-fc7e-4179-8b75-c71c18f98156</Id>
    <AnnotationText>Video_744f36b7-fc7e-4179-8b75-c71c18f98156.mxf</AnnotationText>
    <Hash>8HhnKnLn+Lp/Ik9i94Ml4SXAxH4=</Hash>
    <Size>14568486</Size>
    <Type>application/mxf</Type>
    <OriginalFileName>Video_744f36b7-fc7e-4179-8b75-c71c18f98156.mxf</OriginalFileName>
    <HashAlgorithm Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
  </Asset>
  <Asset>
    <Id>urn:uuid:bf5438ea-ba58-4ae0-a64a-5d23cee2ebb3</Id>
    <AnnotationText>Audio_bf5438ea-ba58-4ae0-a64a-5d23cee2ebb3.mxf</AnnotationText>
    <Hash>Wg4aEAE5Ji9e14ZyGkvfUUjBwCw=</Hash>
    <Size>4341294</Size>
    <Type>application/mxf</Type>
    <OriginalFileName>Audio_bf5438ea-ba58-4ae0-a64a-5d23cee2ebb3.mxf</OriginalFileName>
    <HashAlgorithm Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
  </Asset>
</AssetList>
</PackingList>

代码(部分):

XDocument pkldoc = XDocument.Load(packing, LoadOptions.PreserveWhitespace);

var pklns = pkldoc.Root.GetDefaultNamespace();

var pkluuid = pkldoc.Descendants(pklns + "Id").FirstOrDefault().Value; 

var pklassetElements = pkldoc.Descendants(pklns + "Asset");

foreach (var pklasset in pklassetElements)
{
    var idElement = pklasset.Descendants(pklns + "Id").First();
    if (!idElement.Value.Equals(cpluuid))
                        continue;

    SetNewValue(pklasset, pklns + "OriginalFileName", outfile);

}
void SetNewValue(XElement currentElement, XName elementName, string newValue)
{
    var matchingElements = currentElement.Descendants(elementName);

    if (matchingElements.Any())
         {
         foreach (var element in matchingElements)
                            element.SetValue(newValue);
         }
    }

pkldoc.Save(packing, SaveOptions.DisableFormatting);
FileInfo fi = new FileInfo(packing);
var pklsize = fi.Length;

虽然对我来说不是很干净,但它确实有效。

string text = File.ReadAllText(packing);
text = text.Replace(" />", "/>");
File.WriteAllText(packing, text);

更新

这就是解决方案。谢谢@asherber!

var textToSave = pkldoc.ToString(SaveOptions.DisableFormatting).Replace(" />", "/>"); 
File.WriteAllText(packing, textToSave);