有这样的一个xml
<a>
<b>
<c i="1" />
</b>
<b>
<c j="2" />
<d k="3" />
</b>
</a>
怎么实现简单方便的实现
1.在第二个<b>结构下插入<e l="4" />
2.在第二个<b>结构后面再插入一个<b>结构
是用system.xml命名空间里面的类,还是用system.xml.linq里面的类方便呢?
------解决方案--------------------------------------------------------
system.xml.linq里面的XElement就可以满足你的需求。
------解决方案--------------------------------------------------------
XmlDocument,XmlElement,XmlNode足够处理你的问题了
------解决方案--------------------------------------------------------
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);
root.AppendChild(element);