tags:

views:

58

answers:

2

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&gt;
  <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#?

+3  A: 

You can use XSLT for this. Check out this question: http://stackoverflow.com/questions/144713/add-a-namespace-to-elements-using-xslt

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

Gerrie Schenck
Thanks. Used a mixture of these links and this:http://stackoverflow.com/questions/1778299/simplest-way-to-transform-xml-to-html-with-xslt-in-cWorks fine.
Graeme
A: 

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

Faisal
Don't quite see this - how to add the namespace prefix to each element this way?
Graeme