0

I'm trying to create some xml nodes runtime using XPath for C#. See XML Below:

<Package xmlns:m2="http://schemas.microsoft.com/appx/2013/manifest" xmlns="http://schemas.microsoft.com/appx/2010/manifest">
  <Application>
    <m2:VisualElements>
       <!--- INSERT CHILD NODES HERE WHICH ALSO HAVE NAMESPACE 'm2' ---->
    </m2:VisualElements>
  </Application>
</Package>

Currently I'm doing the following:

XElement visualElements = doc.Descendants().SingleOrDefault(p => p.Name.LocalName == "VisualElements");
visualElements.Add(new XElement(doc.Root.GetDefaultNamespace() + "InitialRotationPreference"));

I know that this is wrong since I reference the default namespace, this will result in this being added:

<InitialRotationPreference />

When I want:

<m2:InitialRotationPreference />

Is there some way to access the parent-nodes namespace (m2) without "knowing" the prefix or the namespace-url?

Thank you!

1 Answer 1

1

Your document root's namespace is http://schemas.microsoft.com/appx/2010/manifest. Use the one from VisualElements:

XName name = visualElements.Name.Namespace + "InitialRotationPreference"

Or specify explicitly:

XName name = XName.Get("InitialRotationPreference", 
    "http://schemas.microsoft.com/appx/2013/manifest");

Then add an element with that name:

visualElements.Add(new XElement(name));
Sign up to request clarification or add additional context in comments.

2 Comments

Exactly what I was looking for (Name.Namespace)! The other solution you mentioned however, I explicitly said I didn't want. :) Thanks for quick answer, will accept answer in 5 min!
@user2422321 oops, missed that bit! It's an option, anyway!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.