C# 要将xml属性结构转换为xml标记结构吗
我正在尝试将XML属性结构转换为XML标记结构C# 要将xml属性结构转换为xml标记结构吗,c#,xml,C#,Xml,我正在尝试将XML属性结构转换为XML标记结构 <company> <Name value="SomeCompany" /name> <Count value ="500"/> </Company> 预期结果如下: <Company> <Name>SomeCompany</name> <EmployeeCount> 500<EmployyeCount> </Compan
<company>
<Name value="SomeCompany" /name>
<Count value ="500"/>
</Company>
预期结果如下:
<Company>
<Name>SomeCompany</name>
<EmployeeCount> 500<EmployyeCount>
</Company>
某公司
500
以下代码应适用于任何复杂的xml文件。我正在使用Xml Linq:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication124
{
class Program
{
static void Main(string[] args)
{
string input =
"<Company>\n" +
"<Name value=\"SomeCompany\"> </Name>\n" +
"<Count value =\"500\"/>\n" +
"</Company>";
XDocument doc = XDocument.Parse(input);
List<XElement> nodes = doc.Descendants().ToList();
for (int i = nodes.Count - 1; i >= 0; i--)
{
XElement node = nodes[i];
if ((string)node.Attribute("value") != null)
{
node.ReplaceWith(new XElement(node.Name.LocalName, new object[] {
node.Attributes().Where(x => x.Name.LocalName != "value"),
(string)node.Attribute("value"),
node.DescendantNodes()
}));
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序124
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串输入=
“\n”+
“\n”+
“\n”+
"";
XDocument doc=XDocument.Parse(输入);
列表节点=doc.subjects().ToList();
对于(int i=nodes.Count-1;i>=0;i--)
{
XElement节点=节点[i];
if((字符串)node.Attribute(“value”)!=null)
{
ReplaceWith(新的XElement(node.Name.LocalName,新对象[]){
node.Attributes()。其中(x=>x.Name.LocalName!=“value”),
(字符串)node.Attribute(“值”),
node.genderantnodes()
}));
}
}
}
}
}
大家好,我如何才能反向执行相同的操作。我有以下格式的xml SomeCompany 500,我想将其转换为数字?
{"Company":{"Name":{"@value":"someCompany"},"EmployeeCount":{"@value":"500"}
<Company>
<Name>SomeCompany</name>
<EmployeeCount> 500<EmployyeCount>
</Company>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication124
{
class Program
{
static void Main(string[] args)
{
string input =
"<Company>\n" +
"<Name value=\"SomeCompany\"> </Name>\n" +
"<Count value =\"500\"/>\n" +
"</Company>";
XDocument doc = XDocument.Parse(input);
List<XElement> nodes = doc.Descendants().ToList();
for (int i = nodes.Count - 1; i >= 0; i--)
{
XElement node = nodes[i];
if ((string)node.Attribute("value") != null)
{
node.ReplaceWith(new XElement(node.Name.LocalName, new object[] {
node.Attributes().Where(x => x.Name.LocalName != "value"),
(string)node.Attribute("value"),
node.DescendantNodes()
}));
}
}
}
}
}