我正在尝试创建一个.xml的副本,并需要像这样显示它(在需要的地方放置信息):
<FMEA>
<Component>
<Name></Name>
<BasicEvent ID="">
<Name></Name>
<ShortName></ShortName>
<Description></Description>
<Unavailability></Unavailability>
</BasicEvent>
<BasicEvent ID="">
<Name></Name>
<ShortName></ShortName>
<Description></Description>
<Unavailability></Unavailability>
</BasicEvent>
<BasicEvent ID="">
<Name></Name>
<ShortName></ShortName>
<Description></Description>
<Unavailability></Unavailability>
</BasicEvent>
//And so on...
</Component>
</FMEA>
我确信我的类是正确的,但我不确定如何遍历我创建的列表,以便将信息放在正确的位置。我的代码id在下面,我真的很感谢你们能提供的任何帮助。谢谢-K
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
namespace ACW
{
class Program
{
const string FILENAME = @"SmallResults.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
FMEA fmea = new FMEA();
fmea.comps = doc.Descendants("FMEA").Select(x => new Component()
{
Name = (string)x.Element("Name"),
BasicEvent = x.Elements("BasicEvent").Select(y => BasicEvent.FindBasicEvent(y)).ToList()
}).ToList();
foreach(Component comp in fmea.comps)
{
new XmlSerializer(typeof(Component)).Serialize(Console.Out, comp);
//I'm sure something goes here... A foreach loop or something...
}
Console.ReadKey();
}
}
public class Component
{
public string Name { get; set; }
public List<BasicEvent> BasicEvent { set; get; }
}
public class BasicEvent
{
public string Name { set; get; }
public string ShortName { set; get; }
public string Description { set; get; }
public string Unavalability { set; get; }
public static BasicEvent FindBasicEvent(XElement BasicEvent)
{
BasicEvent NewBasicEvent = new BasicEvent();
NewBasicEvent.Name = (string)BasicEvent.Element("Name");
NewBasicEvent.ShortName = (string)BasicEvent.Element("ShortName");
NewBasicEvent.Description = (string)BasicEvent.Element("Description");
NewBasicEvent.Unavalability = (string)BasicEvent.Element("Unavalability");
return NewBasicEvent;
}
}
class FMEA
{
public List<Component> comps { get; set; }
}}
发布于 2016-12-04 04:22:29
复制xml有什么困难?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XDocument duplicate = XDocument.Parse(doc.ToString());
}
}
}https://stackoverflow.com/questions/40950322
复制相似问题