XHTML到XML转换

时间:2014-02-03 19:52:37

标签: .net xml xhtml

我正在寻找有关如何将XHTML转换为非常特定的XML的一些信息。例如,我有以下XHTML示例:

<body>
<div id="divParent" class="header" style="width: 250px; height: 200px;">
  <fieldset id="fldScope" style="left: 5px; width: 240px; top: 5px; height: 60px;">
    <label style="left: 5px; top: 5px;">Reason:</label>
    <select id="selReason">
      <option value="">SELECT ONE:</option>
      <option value="TRAINING">TRAINING</option>
      <option value="OTHER">OTHER</option>
    </select>
  </fieldset>
  <fieldset class="bottomSection">
    <button id="btnClose" accessKey="o" class="webbutton" type="button">
      <u>O</u>K</button>
  </fieldset>
</div>
</body>

我需要转换成这样的东西:

<control controlId="topLevelDiv" controlType="HtmlDiv" controlSearchProperties="id=divParent;class=header">
    <childControls>
        <control controlId="topLevelFieldset" controlType="HtmlFieldSet" controlSearchProperties="id=fldScope">
          <childControls>
            <control controlId="topLevelLabel" controlType="HtmlLabel" controlSearchProperties="InnerText=Reason:">
                <childControls/>
            </control>
            <control controlId="topLevelComboBox" controlType="HtmlComboBox" controlSearchProperties="Id=selReason">
                <childControls>
                    <control controlId="defaultOption" controlType="HtmlListItem" controlSearchProperties="InnerText=SELECT ONE">
                        <childControls/>
                    </control>
                    <control controlId="option1" controlType="HtmlListItem" controlSearchProperties="InnerText=TRAINING">
                        <childControls/>
                    </control>
                    <control controlId="option2" controlType="HtmlListItem" controlSearchProperties="InnerText=Other">
                        <childControls/>
                    </control>
                </childControls>
            </control>
            <control controlId="bottomFieldset" controlType="HtmlFieldSet" controlSearchProperties="class=bottomSection">
                <childControls>
                    <control controlId="okButton" controlType="HtmlButton" controlSearchProperties="Id=btnClose; acessKey=o; type=button" >
                      <childControls></childControls>
                    </control>
                </childControls>
            </control>
          </childControls>
        </control>
    </childControls>
</control>

我有关于如何将各种控件映射到不同控件类型的所有映射。但是当我尝试将XHTML加载为XDocument时(为了提取属性和元素),我得到了解析错误。

我想到了正则表达式和基本的字符串操作,但这可能变得难以管理,尤其是在尝试覆盖所有边缘情况时。

我不确定,最好的办法是什么。请帮忙!!

提前致谢。

1 个答案:

答案 0 :(得分:1)

从技术上讲,XHTML已经 IS XML。因此,您无法真正将XHTML转换为XML,但您可以使用XSLT将XML从一个样式表转换为另一个样式表(您可以将其视为定义的转换或{{ 1}},但它并不完全相同)。

您可以在此处了解如何应用XSLT:How to apply an XSLT Stylesheet in C#

以下是如何写一个:beginner XSLT tutorial

如果出现解析错误,请尝试将文本加载到浏览器中。一些(Firefox)将告诉您文档在哪里违反XML合规性。或者,在这里发布错误。

或者看看W3C XHTML Validator告诉你的内容。