Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

c# - Find html attributes without quotes, and add them back in

I have a project where I am outputting content from a CMS into XML.. I don't fully control the content the CMS, and we now have a problem that certain content doesn't fully conform to XML

    <Block PageGuid="xxx" PageId="1234" PageType="block" PageName="blockpage" PageUrl="/en/New-Folder7/New-Folder8/" CreateBlock="false">
  <Properties>
    <Property PropertyName="EmbedCode" Ignore="false" DefaultLanguageChanged="true" TranslatedChanged="true">
      <DefaultLanguage><DIV id=TA_sss class=TA_sss><UL id=sdfsdfsdfsdf class="TA_links xx"><LI id=sdfsdfsf class=sdfsfsf><A href="http://www.tripadvisor.co.uk/">xxxxxxxxx</A></LI></UL></DIV><SCRIPT src="http://www.jscache.com/"></SCRIPT></DefaultLanguage>
      <Translation><DIV id=TA_sss class=TA_sss><UL id=xxxx class='TA_links xxx'><LI id=xxxx class=xxxx><A href='http://www.tripadvisor.co.uk/'>xxxxxxxxx</A></LI></UL></DIV><SCRIPT src='http://www.jscache.com/'></SCRIPT></Translation>
      <PreviousValues>
        <PreviousDefaultText></PreviousDefaultText>
        <PreviousTranslationText></PreviousTranslationText>
      </PreviousValues>
    </Property>
  </Properties>
</Block>

See the above XML.. I need to find any cases where I have an attribute with a missing quote, adding the in:

i.e.

And cases where they are single quotes, replacing with double quotes

i.e.

http://www.tripadvisor.co.uk/'>

I have the entire XML in a string, so I am hoping there is a Regex I can use to do this?

My solution:

            var reader = new StringReader(xml);
        var sgmlReader = new Sgml.SgmlReader
                             {
                                 DocType = "HTML",
                                 WhitespaceHandling = WhitespaceHandling.All,
                                 CaseFolding = Sgml.CaseFolding.ToLower,
                                 InputStream = reader
                             };
        var doc = new XmlDocument { PreserveWhitespace = true, XmlResolver = null };
        doc.Load(sgmlReader);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've used https://github.com/MindTouch/SGMLReader in the past to solve a similar issue. Worked like a charm (YMMV).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...