c#Linq XML - 为什么用引号/命名空间将空格添加到元素?

时间:2018-02-24 00:32:43

标签: c# xml visual-studio linq-to-xml

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

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

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

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;

1 个答案:

答案 0 :(得分:1)

虽然我不是很干净但这很有效。

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

<强>更新

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

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