簡體   English   中英

檢查以base64編碼的XML附件的大小

[英]Checking size of XML attachments encoded in base64

我正在遍歷包含許多XML文件的列表,其中每個文件都附加了以base64編碼的各種文件。 附件模板:

        <my:Attachments>
        <my:Attachment-name>base64code</my:Attachment-name>
        <my:Attachment-name-Description/>
        <my:Attachment-name2>base64code</my:Attachment-name2>
        <my:Attachment-Checklist-Description/>
        <my:Additional-Attachments>
            <my:Attachment>base64code</my:Attachment>
            <my:Attachment-Description>description</my:Attachment-Description>
        </my:Additional-Attachments>
        <my:Additional-Attachments>
            <my:Attachment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">base64code</my:Attachment>
            <my:Attachment-Description>description2</my:Attachment-Description>
        </my:Additional-Attachments>
    </my:Attachments>

我的目標是檢查所有文件中的附件數量以及大小大於提供的附件數量。

我正在用C#進行此操作,但我堅持要自己研究xml文件。 有什么建議我如何遍歷編碼為xml的那些附件,並檢查它們在base64中時的大小?

更新:

根據daniell89的建議,我這樣做:

                    foreach (Microsoft.SharePoint.Client.File file in fileCollection)
                {
                    FileInformation fileInformation =
                        Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, file.ServerRelativeUrl);
                    using (StreamReader streamReader = new StreamReader(fileInformation.Stream))
                    {
                        XDocument xmlDocument = XDocument.Load(streamReader);
                        XNamespace my =
                            XNamespace.Get(
                                "http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-05-03T19:16:34");

                        foreach (XElement xe in xmlDocument.Descendants(my + "Attachments"))
                        {

                        }
                    }
                }

由於附件節點中的每個元素都具有不同的名稱,因此我嘗試遍歷所有元素,如果其值具有base64,則進行其余操作。 問題是,我得到的是1個項目,而不是每個行的集合:my:Attachments中的所有行

您可以使用XDocument類和Linq到xml。 這是一個例子:

string xmlData = "yourXmlString";
int maxAttachementSize = 100000;
XDocument xml = XDocument.Parse(xmlData);
XNamespace my_ns = "myNamespaceDefinition";
var attachementsInBase64String = xml.Descendants(my_ns + "Attachment").Select(x => x.Value);
var tooBigAttachements = attachementsInBase64String .Where(att => Convert.FromBase64String(att).Length > maxAttachementSize);

根據您的更新,您可以通過以下方式檢查附件(假設您有一個附件元素):

foreach (XElement xElem in xDoc.Element("Attachments").Descendants().Where(e => !e.HasElements))
{
      if(!string.IsNullOrWhiteSpace(xElem.Value))
      {
           byte[] attachement = Convert.FromBase64String(xElem.Value);
           // rest of a code
      }
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM