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

XAttribute (LINQ to XML): Elaborate on using the default namespace #44499

Open
SetTrend opened this issue Jan 23, 2025 · 0 comments · May be fixed by #44501
Open

XAttribute (LINQ to XML): Elaborate on using the default namespace #44499

SetTrend opened this issue Jan 23, 2025 · 0 comments · May be fixed by #44501
Labels
dotnet-fundamentals/svc in-pr This issue will be closed (fixed) by an active pull request. ⌚ Not Triaged Not triaged

Comments

@SetTrend
Copy link
Contributor

SetTrend commented Jan 23, 2025

Type of issue

Missing information

Description

The 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:

  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"))

I propose to add an example to the documentation, making clear that XML attributes inherit the enclosing element's default namespace by not prefixing it with the default namespace declaration.

I also created a pull request having one of the examples being amended to include information on how to create XAttribute objects with (a) default namespace, and (b) prefixed namespace applied.


Page URL

https://learn.microsoft.com/en-us/dotnet/standard/linq/create-document-namespaces-csharp

Content source URL

https://github.com/dotnet/docs/blob/main/docs/standard/linq/create-document-namespaces-csharp.md

Document Version Independent Id

e34d6fbd-8233-edcc-d253-def34d2c324f

Article author

@BillWagner

Metadata

  • ID: 525bf456-96f9-3ec5-baa7-231213dff194
  • Service: dotnet-fundamentals

Related Issues

@dotnet-policy-service dotnet-policy-service bot added the ⌚ Not Triaged Not triaged label Jan 23, 2025
@dotnet-policy-service dotnet-policy-service bot added the in-pr This issue will be closed (fixed) by an active pull request. label Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotnet-fundamentals/svc in-pr This issue will be closed (fixed) by an active pull request. ⌚ Not Triaged Not triaged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant