Click here to Skip to main content
15,883,817 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Thanks for the help in advance. I have this sample xml document. I would like to select the Address element with all its children and replace it with <Sam />. In other words, I would like to replace <Sam /> with <Address> ……</Address>
XML
<Contact>
    <Name>Patrick Hines</Name>
    <Phone>206-555-0144</Phone>
    <Address>
      <Street1>123 Main St</Street1>
      <City><city w:st="on"><place w:st="on">Mercer Island</place></city></City>
      <State>WA</State>
      <Postal>68042</Postal>
    </Address>
     <Date>
      <FirstDate>7/23/2011</FirstDate>
      <LastDate>6/20/2012</LastDate>
    </Date>
  <Sam />
  </Contact
Posted
Updated 17-Jul-12 4:56am
v2
Comments
[no name] 17-Jul-12 10:54am    
Did you bother reading your question before clicking the Submit button?
Richard MacCutchan 17-Jul-12 10:57am    
Whichever way round you want to do it, what is your problem?
Sandeep Mewara 17-Jul-12 11:01am    
I am not clear on output expected even thought question states what you are trying. I am not clear on what/why too.
mzammari 17-Jul-12 11:12am    
I would like to select all elements in ("Contact/Address") which would give me a collection of elements. now I would like to take this collection and replace Sam element with this collection:

XDocument doc = XDocument.Load(filePath);

IEnumerable "<"XElement">" Ad = doc.XPathSelectElements("/Contact/Address");

and after that I am lost.
Matt T Heffron 17-Jul-12 13:47pm    
Can you show what the xml should be AFTER this?
It appears that you want the Street1, City, State, Postal elements to be SPLICED into where the Sam element is? Do you want them to be COPIED or MOVED?

1 solution

Ok, from your last comment above, I think I know what you want. (It isn't either of the interpretations that I thought you wanted.)
First your XPath expression was off by a bit.
Then you just needed to copy the elements and Add them to the Sam element.
C#
XDocument doc = XDocument.Load(filePath);
IEnumerable<XElement> Ad = doc.XPathSelectElements("/Contact/Address/*");
var sam = doc.XPathSelectElement("/Contact/Sam");
foreach (var item in Ad)
{
  var newItem = new XElement(item);
  sam.Add(newItem);
}

At this point doc is:
XML
<contact>
  <Name>Patrick Hines</Name>
  <Phone>206-555-0144</Phone>
  <Address>
    <Street1>123 Main St</Street1>
    <City>
      <city w:st="on">
        <place w:st="on">Mercer Island</place>
      </city>
    </City>
    <State>WA</State>
    <Postal>68042</Postal>
  </Address>
  <Date>
    <FirstDate>7/23/2011</FirstDate>
    <LastDate>6/20/2012</LastDate>
  </Date>
  <Sam>
    <Street1>123 Main St</Street1>
    <City>
      <city w:st="on">
        <place w:st="on">Mercer Island</place>
      </city>
    </City>
    <State>WA</State>
    <Postal>68042</Postal>
  </Sam>
</Contact>

An additional comment: This XML design is not ideal in that it is easy for humans to mis-read it, due to the <City> and <city> that differ only by the case of the first letter.
If you can, I'd suggest redesigning this for better readability.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900