5

I have an XML document from which I need to extract a nodeset and add a namespace. So, from a doc I extract this:

<List>
  <ListItem>
    <SomeData>Here is some text</SomeText>
  </ListItem>
  <ListItem>
    <SomeData>Here is some more text</SomeText>
  </ListItem>
</List>

and need to create this:

<my:List xmlsns:my='http://SomeNamespace.org>
  <my:ListItem>
    <my:SomeData>Here is some text</my:SomeText>
  </my:ListItem>
  <my:ListItem>
    <SomeData>Here is some more text</my:SomeText>
  </my:ListItem>
</my:List>

There will be a variable quantity of list items and the elements might change and have different name, so I need a generic solution. Is there an easy way to do that in .Net C#?

Graeme
  • 2,597
  • 8
  • 37
  • 50

2 Answers2

3

You can use XSLT for this. Check out this question: Add a namespace to elements

Use the .net class XslTransform to do this in code: http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=63

Community
  • 1
  • 1
Gerrie Schenck
  • 22,148
  • 20
  • 68
  • 95
  • Thanks. Used a mixture of these links and this: http://stackoverflow.com/questions/1778299/simplest-way-to-transform-xml-to-html-with-xslt-in-c Works fine. – Graeme Feb 08 '10 at 15:29
0

Use http://msdn.microsoft.com/en-us/library/system.xml.xmlnamespacemanager.addnamespace.aspx

Faisal
  • 4,054
  • 6
  • 34
  • 55