0

the file is

<?xml version="1.0" encoding="UTF-8"?>

<opml version="1.0">
    <head>
        <title>subscriptions</title>
    </head>
    <body>

        <outline text="Tech" title="Tech">
            <outline type="rss" text="The Verge" title="The Verge" xmlUrl="http://www.theverge.com/rss/full.xml" htmlUrl="https://www.theverge.com/"/>
        </outline>

    </body>
</opml>

and by using the code to extract the attributes from the readed content

public List<FeedsMainMenuItem> Import(string uri)
{
    XDocument doc = XDocument.Parse(uri);
    //XDocument doc = XDocument.Load(uri);
    List<FeedsMainMenuItem> t = doc
                           .Descendants("outline")
                           .Elements("outline")
                           .Select(o => new FeedsMainMenuItem
                           {
                               FeedUrl = o.Attribute("xmlUrl").Value,
                               Title = o.Attribute("title").Value,
                               SiteUrl = o.Attribute("htmlUrl")?.Value,
                               Favicon = "https://www.google.com/s2/favicons?domain_url=" + o.Attribute("htmlUrl")?.Value

                           })
                           .ToList();
    return t;
}

but the "htmlUrl" attribute value can't be readed

SiteUrl = o.Attribute("htmlUrl")?.Value

So, why even the value is there and what's the solution

Edit: the FeedsMainMenuItem class

public class FeedsMainMenuItem {

[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string FeedUrl { get; set; }
public string Title { get; set; }
public string Favicon { get; set; }
public string SiteUrl { get; set; }
public DateTime LastUpdated { get; set; }

[Ignore]
public Type ViewModelToLoad
{
    get { return JsonConvert.DeserializeObject<Type>(ViewModelToLoadString) ?? typeof(RssFeedViewModel); }
    set { ViewModelToLoadString = JsonConvert.SerializeObject(value); }
}

public string ViewModelToLoadString { get; set; }

//public Type ViewModelToLoad { get; set; } = typeof(RssFeedViewModel);

[OneToMany]
public List<RssSchemaExtended> FeedsItems { get; set; }

}

Sherif Awad
  • 171
  • 2
  • 10

1 Answers1

0

it works if you FeedsMainMenuItem you generated as below

// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class outline
{

    private outlineOutline outline1Field;

    private string textField;

    private string titleField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("outline")]
    public outlineOutline outline1
    {
        get
        {
            return this.outline1Field;
        }
        set
        {
            this.outline1Field = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string text
    {
        get
        {
            return this.textField;
        }
        set
        {
            this.textField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string title
    {
        get
        {
            return this.titleField;
        }
        set
        {
            this.titleField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class outlineOutline
{

    private string typeField;

    private string textField;

    private string titleField;

    private string xmlUrlField;

    private string htmlUrlField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string type
    {
        get
        {
            return this.typeField;
        }
        set
        {
            this.typeField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string text
    {
        get
        {
            return this.textField;
        }
        set
        {
            this.textField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Title
    {
        get
        {
            return this.titleField;
        }
        set
        {
            this.titleField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string FeedUrl
    {
        get
        {
            return this.xmlUrlField;
        }
        set
        {
            this.xmlUrlField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string SiteUrl
    {
        get
        {
            return this.htmlUrlField;
        }
        set
        {
            this.htmlUrlField = value;
        }
    }
}

I have tested with this code

 public static void Import(string uri)
        {
           XDocument doc = XDocument.Load(uri);
            List <outlineOutline> t = doc
                                   .Descendants("outline")
                                   .Elements("outline")
                                   .Select(o => new outlineOutline
                                   {
                                       FeedUrl = o.Attribute("xmlUrl").Value,
                                       Title = o.Attribute("title").Value,
                                       SiteUrl = o.Attribute("htmlUrl")?.Value,

                                   })
                                   .ToList();

            Console.WriteLine("FeedUrl" + t[0].SiteUrl);
        }

Please let me know if you require any help.

Prakash
  • 813
  • 1
  • 8
  • 20
  • why i can get the value of xmlUrl and title without generate FeedsMainMenuItem as you specified – Sherif Awad May 17 '20 at 20:19
  • Hi if you can provide `FeedsMainMenuItem` I can check without code I can not help. – Prakash May 18 '20 at 05:00
  • Boss even if I use `FeedsMainMenuItem` I can see the result in `SiteUrl = o.Attribute("htmlUrl")?.Value` it is possible that you are doing something different than specified in the your code sample – Prakash May 18 '20 at 16:34