Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using default namespace with System.Xml.Linq.XAttribute explained. #44501

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

SetTrend
Copy link
Contributor

@SetTrend SetTrend commented Jan 23, 2025

Summary

The current documentation on "How to create a document with namespaces in C# (LINQ to XML)" doesn't explain how to handle the creation of new XAttribute objects that are supposed to have the enclosing XElement's default namespace applied, e.g.:

<root xmlns="default/namespace">
  <element attribute="value"/>
</root>

Without documentation, there are two options that might be hypothetically applicable for creating the correct XML with LINQ to XML in C#:

  1. XNamespace ns = new XNamespace("default/namespace");
    ...
    new XElement(ns + "element", new XAttribute(ns + "attribute", "value"))
  2. XNamespace ns = new XNamespace("default/namespace");
    ...
    new XElement(ns + "element", new XAttribute("attribute", "value"))

This pull request amends one of the examples by including information on how to create XAttribute objects applying to a default namespace.

Fixes #44499
and dotnet/runtime#111703


Internal previews

📄 File 🔗 Preview link
docs/standard/linq/create-document-namespaces-csharp.md How to create a document with namespaces in C# (LINQ to XML)

@SetTrend SetTrend requested a review from a team as a code owner January 23, 2025 20:16
@dotnetrepoman dotnetrepoman bot added this to the January 2025 milestone Jan 23, 2025
@dotnet-policy-service dotnet-policy-service bot added dotnet-fundamentals/svc community-contribution Indicates PR is created by someone from the .NET community. labels Jan 23, 2025
Copy link
Contributor

@gewarren gewarren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. @BillWagner Do you want to look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community-contribution Indicates PR is created by someone from the .NET community. dotnet-fundamentals/svc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

XAttribute (LINQ to XML): Elaborate on using the default namespace
2 participants