Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8192571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:12:39+00:00 2026-06-07T04:12:39+00:00

I’m a Reactive Extension beginner. Gideon Engelberth gave me excellent answer about Reactive Extension

  • 0

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

How to convert img url to BASE64 string in HTML on one method chain by using LINQ or Rx

Now I have second question that How to assign IObservable return value to a XDocument instance.

Gideon gave me bellow sample.

    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);
    }

I’d like to do like this. Bud It seems to be hard…

public void Convert(XDocument input, out XDocument output)
{
    output = ReplaceImageLinks(input);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:12:39+00:00Added an answer on June 7, 2026 at 4:12 am

    So what you have here is only half useful. It seems the Gideon has answered your question but, perhaps didnt know your extra requirements. So I assume that you want take an XDoc, convert all of the Image src attributes from a path to the BASE64 conent that the path represents. You then want to return that new XDoc object once all the processing is done. Correct?

    If that is the case, then you could do this.

    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();
    }
    

    Which I just tested with

    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(); 
    

    However, there are things wrong with this. 1st, we are mutating state. This is not a great start when dealing with concurrency. We really should be returning a new XDoc that has the correct values. 2nd this seem much more of a TPL problem than an Rx one. Rx is best for dealing with streaming of incoming data, what you have here is a need to perform parallel processing. I think a far better option would be to just task.

    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;
        });
    }
    

    See the the section of my book When to Use Rx in IntroToRx.com

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't have much knowledge about the IPv6 protocol, so sorry if the question
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.