Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have an XElement, resulting from a transform that looks like the following.

<src:Person xmlns:src="http://www.palantir.za">
  <src:Name>Jenifer Harvey</src:Name>
  <src:BirthDate>1969-11-13</src:BirthDate>
  <src:IdentityNumber>6906678550017</src:IdentityNumber>
  <src:Sex>Male</src:Sex>
</src:Person> 

I would like to transform this XElement into a new XElement that has 'src' as a default namespace rather than a 'named' namespace as above.

I realise they are probably semantically identical, I just want to maintain consistency in what is being stored.

I want the following.

<Person xmlns="http://www.palantir.za">
        <Name>Jenifer Harvey</Name>
        <BirthDate>1969-11-13</BirthDate>
        <IdentityNumber>6906678550017</IdentityNumber>
        <Sex>Male</Sex>
</Person> 

Thanks

Regards

Craig.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.7k views
Welcome To Ask or Share your Answers For Others

1 Answer

Simply remove the xmlns:src attribute and add a new xmlns attribute:

XDocument xdoc = XDocument.Parse(
        "<src:Person xmlns:src="http://www.palantir.za">" +
        "  <src:Name>Jenifer Harvey</src:Name>" +
        "  <src:BirthDate>1969-11-13</src:BirthDate>" +
        "  <src:IdentityNumber>6906678550017</src:IdentityNumber>" +
        "  <src:Sex>Male</src:Sex>" +
        "</src:Person>");

xdoc.Root.Add(new XAttribute("xmlns", "http://www.palantir.za"));
xdoc.Root.Attributes(XNamespace.Xmlns + "scr").Remove();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...