none
How to read XML attribute enclosed with single quote containing double quotes RRS feed

  • Question

  • I have a xml node that looks like this

    <SearchItem SearchFor='\%2\%6_*.*"' ReplaceWith='\%2\" -wc"%6_*.*"'></SearchItem>

    When I use Xdocument.Load(xmlFilename.xml) it stops the attribute value at the double quote instead of the ending single quote.

    <SearchItem SearchFor="/MonthlyReports/"" ReplaceWith="MonthlyReports/%2/"" />

    Am I using the wrong reader or library?  I played around with XmlReaderSettings but couldn't find anything that worked.

    XmlReaderSettings settings = new XmlReaderSettings()
                            {
                                
                            };
    
                            using (XmlReader xr = XmlReader.Create(configInfo.ConfigFile, settings))
                            {
                                XDocument d1 = XDocument.Load(xr, LoadOptions.PreserveWhitespace);
                            }
                           //XDocument d1 = XDocument.Load(new XmlReader reader, LoadOptions options);
    
                            XDocument doc = XDocument.Load(configInfo.ConfigFile);
    


    Greg Kowieski The World On-Line

    Thursday, April 28, 2016 11:00 PM

Answers

  • Hello Greg,

    I got it to work (I think) by just changing the line

    foreach (XNode node in searchItemsXml.Nodes())

    to

    foreach (XElement node in searchItemsXml)


    Jeff

    • Marked as answer by Greg Kowieski Friday, April 29, 2016 4:13 PM
    Friday, April 29, 2016 3:00 AM

All replies

  • Hello Greg,

    Do you have a full working sample?  I don't really understand the issue so I am guessing it is to do with the double "" in the SearchFor...

    The XmlReader should read a well structured xml file.  If you post a sample xml that cause the reader to fail, I can have a look.

    Cheers


    Jeff

    Friday, April 29, 2016 12:43 AM
  • <?xml version="1.0" encoding="utf-8" ?>
    <Config Client="MojoMrTuUtilities" Version="11.1.1">
      <SearchItems>
        <SearchItem SearchFor='\%2\%6_*.*"' ReplaceWith='\%2\" -wc"%6_*.*"'></SearchItem>
      </SearchItems>
      <SkipFiles>
      </SkipFiles>
    </Config>


     internal static SearchAndReplaceItems GetSearchItemsFromConfig(ConfigFileInfo configInfo)
            {
                SearchAndReplaceItems searchItems = new SearchAndReplaceItems() { };
                Dictionary<string, string> dic = new Dictionary<string, string>() { };
    
                try
                {
                    IEnumerable<XElement> searchItemsXml = ConfigHelper.GetConfigInfo(configInfo, Descriptor.Messages.TagsSearchItems);
                    if (searchItemsXml == null)
                        throw new ArgumentException(Descriptor.Messages.ExceptionSearchItemMissing);
                    else
                    {
                        foreach (XNode node in searchItemsXml.Nodes())
                        {
                            if (node is XElement)
                            {
                                XElement x = (XElement)node;
                                dic.Add(x.Attribute(Messages.AttSearchFor).Value, x.Attribute(Messages.AttReplaceWith).Value);
                            }
                        }
                    }
                    searchItems.SearchItems = dic.Select(r => r.Key).ToArray();
                    searchItems.ReplaceItems = dic.Select(r => r.Value).ToArray();
    
                    return searchItems;
                }
                catch (Exception)
                {
    
                    throw;
                }
            }


          public static IEnumerable<XElement> GetConfigInfo(ConfigFileInfo configInfo, string xPression)
            {
                try
                {
                    if (configInfo.ConfigFile == string.Empty || configInfo.ConfigFile == null)
                        throw new ArgumentException("configFile");
    
                    if (VerifyConfig(configInfo))
                    {
                        try
                        {
                            XmlReaderSettings settings = new XmlReaderSettings()
                            {
                                
                            };
    
                            using (XmlReader xr = XmlReader.Create(configInfo.ConfigFile, settings))
                            {
                                XDocument d1 = XDocument.Load(xr, LoadOptions.PreserveWhitespace);
                            }
                           //XDocument d1 = XDocument.Load(new XmlReader reader, LoadOptions options);
    
                            XDocument doc = XDocument.Load(configInfo.ConfigFile);
    
                            return doc.Root.XPathSelectElements(xPression);
    
                        }
                        catch (NullReferenceException)
                        {
                            throw new NullReferenceException(xPression + " Element not found in Config.Xml. ");
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                    else
                    {
                        throw new ApplicationException("Error in validating the config xml file.");
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    

    That's all there really is to it.  I expected the reader to pull out the contents inclusive of the double quotes between the start single quote and the end single quote.

    Thanks for giving me a second set of eyes.  Its been a long week...


    Greg Kowieski The World On-Line

    Friday, April 29, 2016 2:33 AM
  • Hello Greg,

    I got it to work (I think) by just changing the line

    foreach (XNode node in searchItemsXml.Nodes())

    to

    foreach (XElement node in searchItemsXml)


    Jeff

    • Marked as answer by Greg Kowieski Friday, April 29, 2016 4:13 PM
    Friday, April 29, 2016 3:00 AM
  • Well, this is interesting.  my code works fine on my laptop, but not on my client's dev box.  Weird.  it's probably some encoding or something like that.  Thanks for your help regardless.  Ima give you credit for your efforts.  

    Greg Kowieski The World On-Line

    Friday, April 29, 2016 4:13 PM