Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何读取xml属性的内容';s值,作为流_C#_.net_Stream_Xmlreader - Fatal编程技术网

C# 如何读取xml属性的内容';s值,作为流

C# 如何读取xml属性的内容';s值,作为流,c#,.net,stream,xmlreader,C#,.net,Stream,Xmlreader,我有一个xml文档,它可能相当大。xml的实际模式非常基本,但其中一个属性的内容可能非常大,我需要在不将整个文档加载到内存的情况下提取它。我还需要对属性值进行base64解码。我计划使用CryptoStream进行Base64解码,因为它可以解码编码数据流,而无需加载内存中的所有内容 我从XmlReader开始,能够在xml中导航,直到找到所需的属性 因为加密流需要一个源流,而XmlReader并没有真正为我提供一个可以读取单个属性内容的源流,所以我认为我需要编写一个流类,在调用流的read方法

我有一个xml文档,它可能相当大。xml的实际模式非常基本,但其中一个属性的内容可能非常大,我需要在不将整个文档加载到内存的情况下提取它。我还需要对属性值进行base64解码。我计划使用CryptoStream进行Base64解码,因为它可以解码编码数据流,而无需加载内存中的所有内容

我从XmlReader开始,能够在xml中导航,直到找到所需的属性

因为加密流需要一个源流,而XmlReader并没有真正为我提供一个可以读取单个属性内容的源流,所以我认为我需要编写一个流类,在调用流的read方法时可以从XmlReader中提取,以便在流中只获取属性值。我想知道的是,如何从XmlReader中读取字节[],以chucks为单位

using (XmlReader reader = XmlReader.Create(Request.Body))
{
    reader.MoveToContent();

    bool dataFound = false;         

    //find the data element
    while (reader.Read())
    {
        if (reader.NodeType == XmlNodeType.Element && reader.LocalName.Equals("data"))
        {
            bDataFound = true;
            //find the value attribute
            if (reader.MoveToAttribute("value"))
            {
                //decode the attribute value, which could be large, we need to read it a s a stream 

                var strAttributeValueStream = new XmlReaderContentStream(reader);
                var strContentDecoded = new CryptoStream(strAttributeValueStream, new FromBase64Transform(), CryptoStreamMode.Read);
                break;
            }
            else
            {
                throw new Exception("Element 'data' does not have a value attribute.");
            }
        }
    }

    if (!dataFound)
    {
        throw new Exception("Element 'data' not found.");
    }
}



public class XmlReaderContentStream : Stream
{
    private XmlReader reader_m;

    public XmlReaderContentStream(XmlReader reader)
    {
        if (reader == null)
        {
            throw new ArgumentNullException(nameof(reader), $"Reader is null.");
        }
        reader_m = reader;
    }

    ...

    public override int Read(byte[] buffer, int offset, int count)
    {
        //this would work, but it may not be base 64 encoded content.  I just want the bytes that represent the literal content.
        return reader_m.ReadContentAsBase64(buffer, offset, count);    
                
    }

    ...
}

我不相信任何常规的.Net Xml解析器都会让您访问属性的字节-所有这些解析器都会给您一个字符串,并且不能保证底层流位于您要查找的属性的开头。。。因此,如果您无法使用字符串,您可能需要找到另一个解析器,并根据您的需要对其进行自定义。我不相信任何常规的.Net Xml解析器都会让您访问属性的字节-所有这些解析器都会给您一个字符串,并且无法保证底层流位于您要查找的属性的开头。。。所以,如果您不能使用字符串,您可能需要找到另一个解析器,并根据您的需要对其进行自定义。