Solved

xdocument addelement 'next to'

Posted on 2010-09-23
3
391 Views
Last Modified: 2012-06-27
I know the order of xml does not matter to the xsd, but it does matter to the human eye and people using MapForce to build XSLT-s.

Given:
<myxml>
 <housenumber>123<housenumber>
<streetsuffix>NE<streetsuffix>
<streetname>Madison<streetname>
<streetpostfix>Dr<streetpostfix>
<city>NY<city>
<state>NY<state>
<zip>12401<zip>
</myxml>

How do I code this statement
Just below 'streetpostfix' add new node called 'concatinatedStreetName'.
If I just do an addelement with 'streetpostfix' as the parent, then the new element is a child of 'streetpostfix'.
If I do an addelement with 'myxml' as the parent, the new node appears below the 'zip' element.  I am trying to put the xml where it will be easiest for people to find it.

My new xml should look like this;
<myxml>
 <housenumber>123<housenumber>
<streetsuffix>NE<streetsuffix>
<streetname>Madison<streetname>
<streetpostfix>Dr<streetpostfix>
<'concatinatedStreetName'> has all the value of [street] in one element</concatinatedStreetName'>
<city>NY<city>
<state>NY<state>
<zip>12401<zip>
</myxml>

Thanks
0
Comment
Question by:wilfordrocks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
kris_per earned 500 total points
ID: 33743390

Find the <streetpostfix> element first and then add the new element after that using XElement.AddAfterSelf method. Code below:

string xml = @"<myxml>
                         <housenumber>123</housenumber>
                        <streetsuffix>NE</streetsuffix>
                        <streetname>Madison</streetname>
                        <streetpostfix>Dr</streetpostfix>
                        <city>NY</city>
                        <state>NY</state>
                        <zip>12401</zip>
                        </myxml>";

            TextReader tr = new StringReader(xml);
            XDocument xdoc = XDocument.Load(tr);

            XElement f = xdoc.Root.Elements().Where(e => e.Name.ToString() == "streetpostfix").First();   
         
            f.AddAfterSelf(new XElement("concatinatedStreetName", "all street values here"));

            Console.WriteLine(xdoc);

Open in new window

0
 
LVL 16

Expert Comment

by:kris_per
ID: 33743470

If you want the code fot making the full street value as well and use in the above code, here it is:
static void Main()
        {
            string xml = @"<myxml>
                         <housenumber>123</housenumber>
                        <streetsuffix>NE</streetsuffix>
                        <streetname>Madison</streetname>
                        <streetpostfix>Dr</streetpostfix>
                        <city>NY</city>
                        <state>NY</state>
                        <zip>12401</zip>
                        </myxml>";

            TextReader tr = new StringReader(xml);
            XDocument xdoc = XDocument.Load(tr);
            XElement f = xdoc.Root.Elements().Where(e => e.Name.ToString() == "streetpostfix").First();

            string fullStreet = GetFullStreet(xdoc);
            f.AddAfterSelf(new XElement("concatinatedStreetName", fullStreet));

            Console.WriteLine(xdoc);
        }

        public static string GetFullStreet(XDocument xdoc)
        {
            string fullStreet = string.Empty;
            var streetValues = xdoc.Root.Elements().Where(e => e.Name.ToString().StartsWith("street")).Select(s => s.Value);
            foreach (string sValue in streetValues)
            {
                if (fullStreet.Length > 0) fullStreet += " ";
                fullStreet += sValue;
            }
            return fullStreet;
        }

Open in new window

0
 

Author Closing Comment

by:wilfordrocks
ID: 33743912
Your 1st response was so ON.
thanks
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question