Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9193805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:15:09+00:00 2026-06-17T21:15:09+00:00

I have an XML document like this: <magento_api> <data_item> <code>400</code> <message>Attribute weight is not

  • 0

I have an XML document like this:

<magento_api>
    <data_item>
        <code>400</code>
        <message>Attribute weight is not applicable for product type Configurable Product</message>
    </data_item>
    <data_item>
        <code>400</code>
        <message>Resource data pre-validation error.</message>
    </data_item>
    <data_item>
        <code>1</code>
        <message></message>
    </data_item>
    <data_item>
        <code></code>
        <message>No code was given</message>
    </data_item>
</magento_api>

I’m trying to iterate each node and do the following:

  1. Throw out any elements that are empty/blank.
  2. Generate new Node with only elements containing values.
  3. Send the resulting doc to different web service.

The part I’m struggling with is how to iterate through each node and check each element for null values.

I’ve been testing this code out at http://rextester.com/runcode but can’t seem to figure it out:

Console.WriteLine("Querying tree loaded with XElement.Load");
Console.WriteLine("----");
XElement doc = XElement.Parse(@"<magento_api>
          <data_item>
            <code>400</code>
            <message>Attribute weight is not applicable for product type Configurable Product</message>
          </data_item>
          <data_item>
            <code>400</code>
            <message>Resource data pre-validation error.</message>
          </data_item>
          <data_item>
            <code>1</code>
            <message></message>
          </data_item>
          <data_item>
            <code></code>
            <message>No code was given</message>
          </data_item>
    </magento_api>");

int counter = 1;
IEnumerable<XNode> nodes =
    from nd in doc.Nodes()
    select nd;
foreach (XNode node in nodes)
{
    Console.WriteLine(counter + "-" + node);
    IEnumerable<XElement> elements =
    from el in node //this is where I've been trying various methods, but no dice.
    select el;
    foreach (XElement e in elements)
    {
           Console.WriteLine(counter + "-" + e.Name + "-" + e.Value + "\r\n");
    }
    counter++;
}

Based on the above XML input, I’m hoping to get the following output:

<magento_api>
    <data_item>
        <code>400</code>
        <message>Attribute weight is not applicable for product type Configurable Product</message>
    </data_item>
    <data_item>
        <code>400</code>
        <message>Resource data pre-validation error.</message>
    </data_item>
    <data_item>
        <code>1</code>
    </data_item>
    <data_item>
        <message>No code was given</message>
    </data_item>
</magento_api>

I’m not sure if I’m using the right methods to iterate over the nodes and elements.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:15:11+00:00Added an answer on June 17, 2026 at 9:15 pm

    A single one-liner could do the job, no need to iterate over all elements. Here it goes:

    doc.Descendants().Where(e => string.IsNullOrEmpty(e.Value)).Remove();
    

    Tester

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        public class TestRemove
        {
            public static void Main() {
                Console.WriteLine("----OLD TREE STARTS---");
                XElement doc = XElement.Parse(@"<magento_api>
                                                  <data_item>
                                                    <code>400</code>
                                                    <message>Attribute weight is not applicable for product type Configurable Product</message>
                                                  </data_item>
                                                  <data_item>
                                                    <code>400</code>
                                                    <message>Resource data pre-validation error.</message>
                                                  </data_item>
                                                  <data_item>
                                                    <code>1</code>
                                                    <message></message>
                                                  </data_item>
                                                  <data_item>
                                                    <code></code>
                                                    <message>No code was given</message>
                                                  </data_item>
                                            </magento_api>");
                Console.Write(doc.ToString());
                Console.WriteLine("");
                Console.WriteLine("----OLD TREE ENDS---");
                Console.WriteLine("");
                doc.Descendants().Where(e => string.IsNullOrEmpty(e.Value)).Remove();
                Console.WriteLine("----NEW TREE STARTS---");
                Console.Write(doc.ToString());
                Console.WriteLine("");
                Console.WriteLine("----NEW TREE ENDS---");
                Console.ReadKey();
            }
        }
    }
    

    And it also could be tested here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a xml document like this : <Node1 attrib1=abc> <node1_1> <node1_1_1 attrib2 =
Hi I have an xml document like this <root> <cert id=1> </cert> <cert id=2>
I have an xml document that looks like this. <foo> <bar type=artist/> Bob Marley
I have an XML document which looks something like this: <meadinkent> <record> <comp_div>MENSWEAR</comp_div> <sty_ret_type>ACCESSORIES</sty_ret_type>
I have an XML document that matches our site navigation something like this: <page
So I have a xml document loaded in JavaScript variable. Xml looks like this:
I have got an XML document which looks something like this. <Root> <Info>....</Info> <Info>....</Info>
I have an xml document like this: <sample xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:type=structure> <film> <!-- The act1,
I have an xml document like this: <?xml version=1.0 encoding=utf-8 ?> <demographics> <country id=1
If I have a XML-document like this <Doc> <File> <ObjectID></ObjectID> </File> </Doc> And it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.