English 中文(简体)
如何向 XDocument 实例指定 IOO 可见 < unit> 返回值
原标题:How to assign IObservable<Unit> return value to a XDocument instance

I m a Reactive Extension beginner. Gideon Engelberth gave me excellent answer about Reactive Extension in my question.

< a href=" https://stackoverflow.com/ questions/8897769/how-to-convert-img-url-to- base64-base64-string-in-html-on-one-method-one-method- by- using- lin" 如何使用 LINQ 或 Rx

现在我要问第二个问题, 如何为 XDocument 实例指定 IOO 可观返回值 。

吉迪安给了我贝洛样本

    public IObservable<Unit> ReplaceImageLinks(XDocument document)
    {
        return (from element in GetImages(document)
                let address = new Uri(element.Attribute("src").Value)
                select (from data in DownloadAsync(address)
                        select Convert.ToBase64String(data)
                       ).Do(base64 => element.Attribute("src").Value = base64)
               ).Merge()
                .IgnoreElements()
                .Select(s => Unit.Default);
    }

我喜欢这样,巴德,这似乎很难...

public void Convert(XDocument input, out XDocument output)
{
    output = ReplaceImageLinks(input);
}
最佳回答

所以,你在这里的只有一半有用。 看来吉迪安已经回答了你的问题, 但是,也许不知道你的额外要求。 所以我假设你想要一个 XDoc, 将所有图像弧属性从路径转换为路径所代表的 BASE64 等量。 然后, 您想要在所有处理完成后返回新的 XDoc 对象 。 正确吗?

如果是这样的话,那么你`他们'可以这样做。

public IObservable<XDocument> ReplaceImageLinks(XDocument document)
{
    return Observable.Create<XDocument>(o=>
    {
        try{
            var images = document.Descendants("Image");
            Parallel.ForEach(images, SwapUriForContent);
            o.OnNext(document);
            o.OnCompleted();
        }
        catch(Exception ex)
        {
            o.OnError(ex);
        }
        return Disposable.Empty;    //Should really be a handle to Parallel.ForEach cancellation token.
    });
}

private static void SwapUriForContent(XElement imageElement)
{
    var address = new Uri(imageElement.Attribute("src").Value, UriKind.RelativeOrAbsolute);
    imageElement.Attribute("src").Value = Download(address);
}

public static string Download(Uri input)
{
    //TODO Replace with real implemenation
    return input.ToString().ToUpper();
}

我刚刚用这个测试过

string str =
    @"<?xml version=""1.0""?>
    <!-- comment at the root level -->
    <Root>
        <Child>Content</Child>
        <Image src=""image.jpg""/>
        <Child>
            <Image src=""image2.jpg""/>
        </Child>
    </Root>";
XDocument doc = XDocument.Parse(str);
ReplaceImageLinks2(doc).Dump(); 

但是,这里有问题。 第一, 我们正在变异状态。 在处理货币时, 这不是一个伟大的开端。 我们真的应该返回一个新的有正确值的 XDoc 。 第二, 这似乎比Rx 1 问题更像是TPL问题。 Rx 最适合处理输入的数据流, 您在这里需要的是进行平行处理。 我认为一个更好的选择就是只执行任务 。

public Task<XDocument> ReplaceImageLinks3(XDocument source)
{
    return Task.Factory.StartNew(()=>
    {
        var copy = new XDocument(source);
        var images = copy.Descendants("Image");
        Parallel.ForEach(images, SwapUriForContent);
        return copy;
    });
}

见我这本书的章节,“http://introtororx.com/Content/v1.0.10621.0/01_whyRx.html#Could” rel=“no follow”>何时使用Rx ,请见http://www.introtoorx.com" rel=“nofollow”>IntroToRx.com

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签