• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

xdocument addelement 'next to'

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
wilfordrocks
Asked:
wilfordrocks
  • 2
1 Solution
 
kris_perCommented:

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
 
kris_perCommented:

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
 
wilfordrocksAuthor Commented:
Your 1st response was so ON.
thanks
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now