Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用c将XML元素转换为属性#_C# - Fatal编程技术网

C# 如何使用c将XML元素转换为属性#

C# 如何使用c将XML元素转换为属性#,c#,C#,我有一个XML,如下所示。在这个XML中,所有属性都可以作为元素使用 <Dress> <ID>001</ID> <shirts> <product> <ID>345</ID> <Name>tee</Name> <Serial>5678</Serial>

我有一个XML,如下所示。在这个XML中,所有属性都可以作为元素使用

<Dress>
    <ID>001</ID>
    <shirts>
        <product>
            <ID>345</ID>
            <Name>tee</Name>
            <Serial>5678</Serial>
        </product>
        <product>
            <ID>456</ID>
            <Name>crew</Name>
            <Serial>4566</Serial>
        </product>       
    </shirts>
    <pants>
        <product>
            <ID>123</ID>
            <Name>jeans</Name>
            <Serial>1234</Serial>
            <Color>blue</Color>
        </product>
        <product>
            <ID>137</ID>
            <Name>skirt</Name>
            <Serial>3455</Serial>
            <Color>black</Color>
        </product>
    </pants>
</Dress>

001
345
球座
5678
456
全体船员
4566
123
牛仔裤
1234
蓝色
137
短裙
3455
黑色
我需要将此XML转换为:

<Dress ID="001">
    <shirts>
        <product ID="345" Name="tee" Serial="5678"/>
        <product ID="456" Name="crew" Serial="4566"/>
    </shirts>
    <pants>
        <product ID="123" Name="jeans" Serial="1243" Color="blue"/>
        <product ID="123" Name="skirt" Serial="3455" Color="black"/>
    </pants>
</Dress>

基本上,我需要将元素转换为属性。如何使用c#实现这一点

private static void replacements swithattributes()
{
字符串xmlData=@”
001
345
球座
5678
456
全体船员
4566
123
牛仔裤
1234
蓝色
137
短裙
3455
黑色
";
var doc=XDocument.Parse(xmlData);
var replaceElementTargets=新字符串[]{“衬衫”、“裤子”};
foreach(replaceElementTargets中的var目标)
{
var product=doc.Root.Elements(target.Elements(“产品”).ToList();
ForEach(p=>p.Elements().ToList().ForEach(e=>{p.SetAttributeValue(e.Name,e.Value);e.Remove();}));
}
var outputXML=doc.ToString();
}

尝试在1以下使用E。它按照您的预期生成输出

using System;
using System.Linq;
using System.Xml.Linq;

namespace CangeOneXmlToAnotherXmlConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var sourceXml = @"<Dress>
                                <ID>001</ID>
                                <shirts>
                                    <product>
                                        <ID>345</ID>
                                        <Name>tee</Name>
                                        <Serial>5678</Serial>
                                    </product>
                                    <product>
                                        <ID>456</ID>
                                        <Name>crew</Name>
                                        <Serial>4566</Serial>
                                    </product>       
                                </shirts>
                                <pants>
                                    <product>
                                        <ID>123</ID>
                                        <Name>jeans</Name>
                                        <Serial>1234</Serial>
                                        <Color>blue</Color>
                                    </product>
                                    <product>
                                        <ID>137</ID>
                                        <Name>skirt</Name>
                                        <Serial>3455</Serial>
                                        <Color>black</Color>
                                    </product>
                                </pants>
                            </Dress>";
            var xmlDoc = XDocument.Parse(sourceXml);
            //Remove the ID element
            var firstChildNodeVal = ((XElement)((XContainer)xmlDoc.FirstNode).FirstNode).Value;
            xmlDoc.Descendants("ID").Remove();
            //Add an attribute(ID) with value to the root element
            xmlDoc.Root.SetAttributeValue("ID", firstChildNodeVal);
            //Define the new elements to be available inside the root element
            var elemetsToBeFormatted = new string[] { "shirts", "pants" };
            //Loop it and add the elements inside root element
            foreach (var item in elemetsToBeFormatted)
            {
                var aitem = xmlDoc.Root.Elements(item).Elements("product").ToList();
                aitem.ForEach(p => p.Elements().ToList().ForEach(e => { p.SetAttributeValue(e.Name, e.Value); e.Remove(); }));
            }
            var expectedXml = xmlDoc.ToString();
            Console.WriteLine(expectedXml);
            Console.Read();
        }
    }
}
使用系统;
使用System.Linq;
使用System.Xml.Linq;
名称空间CangeOneXmlToAnotherXmlConsoleApp
{
班级计划
{
静态void Main(字符串[]参数)
{
var sourceXml=@“
001
345
球座
5678
456
全体船员
4566
123
牛仔裤
1234
蓝色
137
短裙
3455
黑色
";
var xmlDoc=XDocument.Parse(sourceXml);
//删除ID元素
var firstChildNodeVal=((XElement)((XContainer)xmlDoc.FirstNode).FirstNode.Value;
xmlDoc.subjects(“ID”).Remove();
//向根元素添加具有值的属性(ID)
SetAttributeValue(“ID”,firstChildNodeValue);
//定义根元素中可用的新元素
var elemetsToBeFormatted=新字符串[]{“衬衫”、“裤子”};
//循环它并在根元素中添加元素
foreach(elemetsToBeFormatted中的var项)
{
var aitem=xmlDoc.Root.Elements(item.Elements(“产品”).ToList();
ForEach(p=>p.Elements().ToList().ForEach(e=>{p.SetAttributeValue(e.Name,e.Value);e.Remove();}));
}
var expectedXml=xmlDoc.ToString();
Console.WriteLine(expectedXml);
Console.Read();
}
}
}
输出


您确定要修改源文件吗?那为什么要贴标签?是的。必须修改源文件。我需要使用c#来完成此操作。到目前为止您尝试了什么?您找到解决方案了吗?看看我的答案。它会按照您的预期生成输出您缺少根的ID=“001”属性element@u好地方!我不认为这需要很大的改变来解决。
using System;
using System.Linq;
using System.Xml.Linq;

namespace CangeOneXmlToAnotherXmlConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var sourceXml = @"<Dress>
                                <ID>001</ID>
                                <shirts>
                                    <product>
                                        <ID>345</ID>
                                        <Name>tee</Name>
                                        <Serial>5678</Serial>
                                    </product>
                                    <product>
                                        <ID>456</ID>
                                        <Name>crew</Name>
                                        <Serial>4566</Serial>
                                    </product>       
                                </shirts>
                                <pants>
                                    <product>
                                        <ID>123</ID>
                                        <Name>jeans</Name>
                                        <Serial>1234</Serial>
                                        <Color>blue</Color>
                                    </product>
                                    <product>
                                        <ID>137</ID>
                                        <Name>skirt</Name>
                                        <Serial>3455</Serial>
                                        <Color>black</Color>
                                    </product>
                                </pants>
                            </Dress>";
            var xmlDoc = XDocument.Parse(sourceXml);
            //Remove the ID element
            var firstChildNodeVal = ((XElement)((XContainer)xmlDoc.FirstNode).FirstNode).Value;
            xmlDoc.Descendants("ID").Remove();
            //Add an attribute(ID) with value to the root element
            xmlDoc.Root.SetAttributeValue("ID", firstChildNodeVal);
            //Define the new elements to be available inside the root element
            var elemetsToBeFormatted = new string[] { "shirts", "pants" };
            //Loop it and add the elements inside root element
            foreach (var item in elemetsToBeFormatted)
            {
                var aitem = xmlDoc.Root.Elements(item).Elements("product").ToList();
                aitem.ForEach(p => p.Elements().ToList().ForEach(e => { p.SetAttributeValue(e.Name, e.Value); e.Remove(); }));
            }
            var expectedXml = xmlDoc.ToString();
            Console.WriteLine(expectedXml);
            Console.Read();
        }
    }
}