日期:2014-05-17 浏览次数:20866 次
<a>
<b>
<c i="1" />
</b>
<b>
<c j="2" />
<d k="3" />
</b>
</a>
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"c:\test.xml");
XmlElement element = xmlDoc.CreateElement("e");
element.SetAttribute("i", "4");
xmlDoc.SelectSingleNode("/a/b[2]").AppendChild(element);
element = xmlDoc.CreateElement("b");
element.InnerText = string.Empty;
xmlDoc.DocumentElement.AppendChild(element);
xmlDoc.Save(@"c:\test.xml");
static void Test1()
{
string str = "a2b1ci1b2cj2dk3";
Match match = Regex.Match(str, @"^([a-z]\d)([a-z]\d
------解决方案--------------------
[a-z]{2,2}\d)+$");
if (match == null
------解决方案--------------------
!match.Success)
{
throw new InvalidOperationException("字符串无法转换成Xml");
}
XmlDocument doc = new XmlDocument();
Group group = match.Groups[1];
XmlElement root = CreateNode(group.Value, doc);
doc.AppendChild(root);
group = match.Groups[2];
XmlElement element = null;
foreach (Capture capture in group.Captures)
{
if (Regex.IsMatch(capture.Value,@"^[a-z]\d$"))
{
element = CreateNode(capture.Value, doc);
ro