Schema.NET Documentation

repository·main·Indexed 20 days ago

https://github.com/rehansaeed/schema.net

A library that converts Schema.org objects into strongly typed C# POCO classes for representing structured data in JSON-LD or XML formats. It provides tools for SEO and machine-readable metadata, including XSS prevention via ToHtmlEscapedString() and flexible handling of single, multiple, or mixed types using generics and implicit conversion operators.

Tokens
921
Snippets
4
Records
4
Agent score
22%

What's inside Schema.NET

  1. How Schema.NET handles single or multiple values and mixed types

    main

    Schema.NET uses generics and implicit conversion operators (via OneOrMany<T>, Values<T1, T2>, etc.) to allow a single property to accept a single value, a list of values, or different types (e.g., a string or a PostalAddress for an Address property).

    Setting single or multiple values

    // Single string address
    var organization = new Organization() { Address = "123 Old Kent Road E10 6RL" };
    
    // Multiple string addresses
    var organization = new Organization() { Address = new List<string> { "Address 1", "Address 2" } };
    
    // Single complex type (PostalAddress)
    var organization = new Organization() { Address = new PostalAddress { StreetAddress = "123 Old Kent Road" } };

    Handling mixed types

    If a property contains multiple possible types (e.g., an Author that can be a Person or an Organization), you can use deconstruction to separate them:

    var book = new Book()
    {
        Author = new List<object>()
        {
            new Organization() { Name = "Penguin" },
            new Person() { Name = "J.D. Salinger" }
        }
    };
    
    // Deconstruct a property containing mixed types
    if (book.Author.HasValue)
    {
        var (organisations, people) = book.Author.Value;
    }
    // Mixed Author types
    var book = new Book()
    {
        Author = new List<object>()
        {
            new Organization() { Name = "Penguin" },
            new Person() { Name = "J.D. Salinger" }
        }
    };
    
    // Deconstruct a property containing mixed types
    if (book.Author.HasValue)
    {
        var (organisations, people) = book.Author.Value;
    }
  2. Prevent XSS when serializing for HTML scripts

    main

    When outputting serialized schema data into an HTML <script> tag, always use the .ToHtmlEscapedString() method instead of .ToString(). This prevents Cross-Site Scripting (XSS) attacks if any properties in your schema were populated from untrusted sources.

    In an ASP.NET MVC project, use it like this:

    <script type="application/ld+json">
        @Html.Raw(Model.Schema.ToHtmlEscapedString())
    </script>
  3. Install Schema.NET via NuGet

    main

    Schema.NET provides strongly typed C# POCO classes representing Schema.org objects. You can install the standard package for released schema types or the .Pending package if you require types that are currently pending on schema.org.

    • Standard version: Use the Schema.NET NuGet package.
    • Pending version: Use the Schema.NET.Pending NuGet package to access both released and pending schema types.
    # Install standard version
    # dotnet add package Schema.NET
    
    # Or install pending version
    # dotnet add package Schema.NET.Pending
  4. Serialize Schema objects to JSON-LD

    main

    You can create Schema.org objects using strongly typed C# classes and serialize them to JSON-LD by calling .ToString(). This is commonly used to generate structured data for the <head> section of HTML pages.

    var website = new WebSite()
    {
        AlternateName = "An Alternative Name",
        Name = "Your Site Name",
        Url = new Uri("https://example.com")
    };
    var jsonLd = website.ToString();