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 7173065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:44:46+00:00 2026-05-28T15:44:46+00:00

I’m reading an svg/xml with XmlTextReader and I want to write just some of

  • 0

I’m reading an svg/xml with XmlTextReader and I want to write just some of its elements back in a new svg/xml file

<svg>
    <rect x="135.7" y="537.4" fill="none" stroke="#000000" stroke-width="0.2894" width="36.6" height="42.2"/>
    <rect x="99" y="537.4" fill="#A0C9EC" stroke="#000000" stroke-width="0.2894" width="36.7" height="42.2"/>
</svg>

I want the rect elements that are filled with “none” so I started reading and writing

 var reader = new XmlTextReader(file.InputStream);
 var writer = new XmlTextWriter(svgPath + fileNameWithoutExtension + "-less" + extension, null);
 writer.WriteStartDocument();
 while (reader.Read())
 {
    switch (reader.NodeType)
    {
         case XmlNodeType.Element:
         if (reader.Name == "svg")
         {
            writer.WriteStartElement(reader.Name);
            while(reader.MoveToNextAttribute()){
                 writer.WriteAttributes(reader, true);
            }
         }
         if (reader.Name == "rect" || reader.Name == "polygon")
         {
            while(reader.MoveToNextAttribute()){
                 if(reader.Name == "fill" && reader.value == "none"){
                     writer.WriteStartElement(reader.Name);
                     writer.WriteAttributes(reader, true);
                     writer.WriteEndElement();
                 }
            }
         }
         break;
         case XmlNodeType.EndElement:
              break;

         default:
              break;
    }
 }
 writer.WriteEndElement();
 writer.WriteEndDocument();
 writer.Close();

but that returns a file with “fill” elements and no “x” and “y” attributes (because the WriteAttributes read from the current position and the reader is in the fill attribute)

 <svg>
     <fill="none" stroke="#000000" stroke-width="0.2894" width="36.6" height="42.2"/>
 </svg>

Is there a way to get the result I want? I don’t want to use XmlDocument since my svg files are large and take a long time to load with XmlDoc.Load

Thanks

  • 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-05-28T15:44:48+00:00Added an answer on May 28, 2026 at 3:44 pm

    Try the following; you can adapt to suit:

         if (reader.Name == "rect" || reader.Name == "polygon") 
         { 
               string fill = reader.GetAttribute("fill"); //Read ahead for the value of the fill attr
               if(String.Compare(fill, "none", StringComparison.InvariantCultureIgnoreCase) == 0) //if the value is what we expect then just wholesale copy this node.
               {
                    writer.WriteStartElement(reader.Name);
                    writer.WriteAttributes(reader, true); 
                    writer.WriteEndElement(); 
                } 
            } 
    

    Note that GetAttribute does not move the reader, allowing you to stay in the context of the rect or polygon node.

    You could probably also try revising your first attempt with a single reader.MoveToFirstAttribute() – I haven’t tested that but it should move you back to the first element of the current node.

    You might also want to investigate the XPathDocument class – this is a lot lighter than XmlDocument but you still load the entire structure into memory first so may not be suitable – selecting the nodes you want would then simply be a case of using the XPath:

    //rect[@fill='none'] || //polygon[@fill='none'] 
    

    Finally, you could also use an XSLT. If you wanted to post a more complete structure for your source document I’d be happy to write one for you.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
I'm parsing an XML file, the creators of it stuck in a bunch social
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the

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.