我有以下输入xml,需要读取class属性等于ss_form_columns的标签下的值。并且需要放置后读32个字符。
输入XML是:
<html>
<div class="SS_Form_Columns">
<span class="SS_LeftAlign"> City of <span class="SS_Form_Input SS_Form_DotBorder" style="width:11em"/>, City of <span class="SS_Form_Input SS_Form_DotBorder" style="width:11em"/> Police Department, County of <span class="SS_Form_Input SS_Form_DotBorder" style="width:11em"/>, County of <span class="SS_Form_Input SS_Form_DotBorder" style="width:11em"/> Sheriff’s Department, <span class="SS_Form_Input SS_Form_DotBorder" style="width:11em"/> Channel, Inc., <span class="SS_Form_Input SS_Form_DotBorder" style="width:11em"/> Film Co., <span class="SS_Form_Input SS_Form_DotBorder" style="width:11em"/> and <span class="SS_Form_Input SS_Form_DotBorder" style="width:11em"/>, </span>
</div>
</html>
我得到的是低于标准:
<div class="SS_Form_Columns">
City of , City<br_tag></br_tag>of Police Department, County of<br_tag></br_tag>, County of Sheriff’s<br_tag></br_tag>Department, Channel, Inc., Film<br_tag></br_tag>Co., and ,
</div>
要求输出是:
<html>
<div class="SS_Form_Columns">
<span class="SS_LeftAlign"> City of <span class="SS_Form_Input SS_Form_DotBorder" style="width:11em"/>, City of <span class="SS_Form_Input SS_Form_DotBorder" style="width:11em"/> Police<br_tag></br_tag> Department, County of<br_tag></br_tag> <span class="SS_Form_Input SS_Form_DotBorder" style="width:11em"/>, County of <span class="SS_Form_Input SS_Form_DotBorder" style="width:11em"/> Sheriff’s Department, <span class="SS_Form_Input SS_Form_DotBorder" style="width:11em"/> Channel,<br_tag></br_tag> Inc., <span class="SS_Form_Input SS_Form_DotBorder" style="width:11em"/> Film Co., <span class="SS_Form_Input SS_Form_DotBorder" style="width:11em"/> and <span class="SS_Form_Input SS_Form_DotBorder" style="width:11em"/>, </span>
</div>
</html>
我的代码如下:
namespace BrTag
{
class Program
{
static int formTitleLength = 32;
static void Main(string[] args)
{
string path = @"D:\Srinivas\BrTag\XMLFile1.xml";
XDocument doc = new XDocument();
doc = XDocument.Load(new XmlTextReader("D:\\Srinivas\\BrTag\\XMLFile12.xml"), LoadOptions.PreserveWhitespace);
foreach (XElement element in doc.Descendants())
{
if (element.Name.LocalName.ToLower() == "div") //looking for the footnote text
{
try
{
if (element.Attribute("class") != null)
if (element.Attribute("class").Value.ToLower() == "ss_form_columns")
{
XElement elem = element;
if (elem.Value.Length > formTitleLength)
{
int cntr = 0;
string value = "";
ArrayList obj = new ArrayList();
string[] words = elem.Value.Split(new char[] { ' ' });
foreach (string word in words)
{
if ((cntr + word.Length) > formTitleLength)
{
//add the current line of the title to the collection
obj.Add(value);
//create the br_tag element and add it to the collection
XElement brTag = new XElement("br_tag", "");
brTag.Name = XNamespace.None.GetName(brTag.Name.LocalName);
obj.Add(brTag);
//start the next line of the title string
value = word;
cntr = word.Length;
}
else
{
if (cntr == 0)
{
value = word;
cntr += word.Length;
}
else
{
value += string.Format(" {0}", word);
cntr += word.Length + 1; //add 1 to account for space
}
}
}
obj.Add(value);
//use the collection of ojects, title string and br_tag elements, to create a replacement element
XElement ne = new XElement(elem.Name, new XElement(elem.Name, obj.ToArray()),
from a in elem.Attributes() select a);
elem.ReplaceWith(ne);
}
element.ReplaceWith(elem);
//Console.WriteLine(elem);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
}
}
}
}
任何人都可以请你帮忙。