从基于条件的列表中创建一个XML文件 [英] Create a XML File from a list based on a conditional

查看:114
本文介绍了从基于条件的列表中创建一个XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图了解,几个小时,但没有运气。
我有一个基于类的列表:

  public class xmldata //接收项目列表的列表
{
public string xml_filename {get;组; }
public string colorname {get;组; }
public string colorvalues {get;组; }




$ b然后我做了一个分离的类来保存列表:

  public class xmldatalist 
{
public List< xmldata> FullList = new List< xmldata>();



$ b $ p
$ b这个列表的每个位置(FullList)包含xmldata的三个元素 - 我想问的一个问题是xml_filename。我的问题是:我怎么能有一个foreach循环遍历这个列表,并为每个xml_filename值生成一个XML文件。
从位置0到5,xml_filename是file1.bla
从位置6到9,xml_filename是file2.bla



我想要一个将循环遍历位置0到5的foreach,检查xml_filename是否为file1.bla,并写入位于0到5的属性。



类似于:

检查xml_filename
创建文件
写属性
如果xml_filename在下一个位置相同
继续在这个文件上写
如果xml_file在下一个位置不同,写新文件

现在我正在尝试使用Linq,做到这一点:$ b​​
$ b

  foreach(var ListXmlFileName in XMLList.FullList.Where(x => xml_filename == XmlFilename))
{
}

有点不知所措。
我试过的另一种方法是:
$ b $ $ $ $ $ $ $ $ $ b $ $ $ $ $ $ $ $ $
$ b

没有成功。编写XML的代码是:

pre $ public $ write $ xml xmlItems,变量GlobalVars
{
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
IndentChars =\t,
NewLineChars = Environment.NewLine,
NewLineHandling = NewLineHandling.Replace ,
Encoding = new UTF8Encoding(false)
};
int pos = 0;


$ b foreach(XmlItems.ItemsList中的XmlStuff)
{

字符串DesktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory );
string XmlFilename =dadada;
字符串FileExtension =.xml;
string PathString = Path.Combine(DesktopFolder,XML);
System.IO.Directory.CreateDirectory(PathString);
string FullPath = Path.Combine(PathString,XmlFilename + FileExtension);

XmlWriter XmlWriting = XmlWriter.Create(FullPath,settings);

XmlWriting.WriteStartDocument();
XmlWriting.WriteStartElement(JMF);
XmlWriting.WriteAttributeString(SenderID,InkZone-Controller);
XmlWriting.WriteAttributeString(Version,1.2);
//XmlWriting.WriteAttributeString(\"xmlns\",\"\"nn,http://www.CIP4.org/JDFSchema_1_1);

XmlWriting.WriteStartElement(Command);
XmlWriting.WriteAttributeString(ID,cmd.00695);
XmlWriting.WriteAttributeString(Type,Resource);


XmlWriting.WriteStartElement(ResourceCMDParams);
XmlWriting.WriteAttributeString(ResourceName,InkZoneProfile);
XmlWriting.WriteAttributeString(JobID,K_41);


XmlWriting.WriteStartElement(InkZoneProfile);
XmlWriting.WriteAttributeString(ID,r0013);
XmlWriting.WriteAttributeString(Class,Parameter);
XmlWriting.WriteAttributeString(Locked,false);
XmlWriting.WriteAttributeString(Status,Available);
XmlWriting.WriteAttributeString(PartIDKeys,SignatureName SheetName Side Separation);
XmlWriting.WriteAttributeString(DescriptiveName,Schieberwerte von DI);
XmlWriting.WriteAttributeString(ZoneWidth,32);


XmlWriting.WriteStartElement(InkZoneProfile);
XmlWriting.WriteAttributeString(SignatureName,SIG1);

XmlWriting.WriteStartElement(InkZoneProfile);
XmlWriting.WriteAttributeString(Locked,False);
XmlWriting.WriteAttributeString(SheetName,S1);

XmlWriting.WriteStartElement(InkZoneProfile);
XmlWriting.WriteAttributeString(Side,Front);

我也试过,但没有按预期工作(while循环)

  while(XmlStuff.xml_filename == XmlItems.ItemsList [pos] .xml_filename)
{
XmlWriting.WriteStartElement InkZoneProfile);
XmlWriting.WriteAttributeString(Separation,XmlItems.ItemsList [pos] .colorname);
XmlWriting.WriteAttributeString(ZoneSettingsX,XmlItems.ItemsList [pos] .colorvalues);
XmlWriting.WriteEndElement();
pos ++;
}
XmlWriting.WriteEndDocument();
XmlWriting.Close();




$ b $ p $任何帮助将非常感谢。



编辑:XML示例



**。

 <?xml version =1.0encoding =utf-8?> ; 
< Command ID =cmd.00695Type =Resource>
< ResourceCmdParams ResourceName =InkZoneProfileJobID =K_41>
< InkZoneProfile ID =r0013Class =ParameterLocked =falseStatus =AvailablePartIDKeys =SignatureName SheetName Side SeparationDescriptiveName =Schieberwerte von DIZoneWidth =32>
< InkZoneProfile SignatureName =SIG1>
< InkZoneProfile Locked =falseSheetName =S1>
< InkZoneProfile Side =Front>
< InkZoneProfile Separation =designer P& G 1901ZoneSettingsX =0.391 0.36 0.097 0.058 0 0 0 0 0 0 0 0 0.178 0.394 0.201 0.088/>
< InkZoneProfile Separation =designer P& G 0323ZoneSettingsX =0.011 0.028 0.02 0.021 0.079 0.071 0.046 0.059 0.043 0.062 0.044 0.094 0.024 0.037 0.012 0.023/>
< InkZoneProfile Separation =designer P& G 0068ZoneSettingsX =0 0.04 0.03 0.016 0.08 0.005 0.041 0.036 0.034 0.044 0.028 0.072 0.001 0.005 0.063 0.008/>
< InkZoneProfile Separation =designer P& G 0056ZoneSettingsX =0 0.064 0.053 0.032 0.137 0.022 0.164 0.097 0.153 0.11 0.052 0.127 0.01 0.007 0.108 0.008/>
< InkZoneProfile Separation =designer P& G 0306ZoneSettingsX =0 0 0 0.19 0.187 0.259 0.224 0.178 0.24 0.185 0.249 0.188 0.173 0 0 0.004/>
< InkZoneProfile Separation =designer P& G 0016ZoneSettingsX =0.069 0.1 0.21 0.049 0.07 0.027 0.138 0.265 0.134 0.242 0.056 0.066 0.039 0.09 0.17 0.13/>
< InkZoneProfile Separation =designer P& G 0057ZoneSettingsX =0 0 0 0 0.023 0.001 0.012 0.012 0.008 0.016 0.003 0.021 0.001 0 0 0.004/>
< InkZoneProfile Separation =KeylineZoneSettingsX =0.013 0.006 0.006 0.038 0.007 0.033 0.012 0.026 0.018 0.02 0.016 0.007 0.034 0.006 0.006 0.022/>
< / InkZoneProfile>
< / InkZoneProfile>
< / InkZoneProfile>
< / InkZoneProfile>

这会更准确:

 < InkZoneProfile Separation =CyanZoneSettingsX =0.391 0.36 0.097 0.058 0 0 0 0 0 0 0 0 0.178 0.394 0.201 0.088/> 

让我们假设这属于file1。 File1将有五个
SomeColor和SomeValues在列表的每个位置。

解决方案

这就是你想要的:

  [可序列化] 
[XmlType(InkZoneProfile)]
public class xmldata接收物品清单
{
[XmlIgnore]
公共字符串xml_filename {get;组; }

[XmlAttribute(Separation)]
public string colorname {get;组; }

[XmlAttribute(ZoneSettingsX)]
public string colorvalues {get;组; (new xmldata {xml_filename = @D:\File_One,colorname =Red,colorvalues =#FF0000});}


$ b FullList.Add ;
FullList.Add(new xmldata {xml_filename = @D:\File_One,colorname =Blue,colorvalues =#0000FF});
FullList.Add(new xmldata {xml_filename = @D:\File_Two,colorname =Black,colorvalues =#000000});
FullList.Add(new xmldata {xml_filename = @D:\File_Two,colorname =White,colorvalues =#FFFFFF});

Dictionary< string,List< xmldata>> xmlFiles = new Dictionary< string,List< xmldata>>();
foreach(FullList中的var项)
{
if(!xmlFiles.ContainsKey(item.xml_filename))xmlFiles [item.xml_filename] = new List >();
xmlFiles [item.xml_filename] .Add(item);
}

foreach(var i in xmlFiles)
{
string yourXMLFileName = i.Key;
列出< xmldata> xmlDataOfThisFile = i.Value;
Console.WriteLine(yourXMLFileName);
使用(FileStream fs = new FileStream(yourXMLFileName,FileMode.Create,FileAccess.Write,FileShare.None))
尝试
{

{
XmlSerializer serializer = new XmlSerializer的(typeof运算(列表与LT; XMLDATA>));
serializer.Serialize(fs,FullList);

$ b $ catch(Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.InnerException);






$ p $这将创建这个XML:

 <?xml version =1.0?> 
< ArrayOfInkZoneProfile xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns:xsd =http://www.w3.org/2001/XMLSchema>
< InkZoneProfile Separation =RedZoneSettingsX =#FF0000/>
< InkZoneProfile Separation =BlueZoneSettingsX =#0000FF/>
< InkZoneProfile Separation =BlackZoneSettingsX =#000000/>
< InkZoneProfile Separation =WhiteZoneSettingsX =#FFFFFF/>
< / ArrayOfInkZoneProfile>

编辑

<根据你的评论,你有其他问题。如果题目问题现在解决了,你应该问一个新的问题。不要在这里结合它。


I've been trying to understand that for several hours but got no luck. I've a list based on a class:

public class xmldata //Class to receive items list
{
    public string xml_filename { get; set; }
    public string colorname { get; set; }
    public string colorvalues { get; set; }

}

Then i made a separated class to hold the list:

public class xmldatalist
{
    public List<xmldata> FullList = new List<xmldata>();
}

Each position of this list (FullList) contains three elements from xmldata - the specific one i want to focus is xml_filename.

My question is: How could i have a foreach loop to iterate over this list , and generate one XML file for each xml_filename value.

i.e: From positions 0 to 5 , xml_filename is file1.bla From positions 6 to 9 , xml_filename is file2.bla

I want a foreach that will iterate trough positions 0 to 5 , check that xml_filename is file1.bla and write attributes who are on position 0 to 5.

Something like:

Check xml_filename Create the file Write Attributes if xml_filename is the same on the next position keep writing on this file if xml_file is different on the next position, write new file

Right now i was trying to use Linq, by doing this:

foreach (var ListXmlFileName in XMLList.FullList.Where(x => x.xml_filename == XmlFilename))
{
}

But i'm kinda lost on how to proceed. Another approach i tried was:

foreach (xmldata XmlStuff in XmlItems.ItemsList)
{
}

Without success also. The code for writing the XML is:

public void writexml(xmldatalist XmlItems, variables GlobalVars)
{
    XmlWriterSettings settings = new XmlWriterSettings
    {
        Indent = true,
        IndentChars = "\t",
        NewLineChars = Environment.NewLine,
        NewLineHandling = NewLineHandling.Replace,
        Encoding = new UTF8Encoding(false)
    };
    int pos = 0;



    foreach (xmldata XmlStuff in XmlItems.ItemsList)
    {

        string DesktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
        string XmlFilename = "dadada";
        string FileExtension = ".xml";
        string PathString = Path.Combine(DesktopFolder, "XML");
        System.IO.Directory.CreateDirectory(PathString);
        string FullPath = Path.Combine(PathString, XmlFilename + FileExtension);

        XmlWriter XmlWriting = XmlWriter.Create(FullPath, settings);

            XmlWriting.WriteStartDocument();
            XmlWriting.WriteStartElement("JMF");
            XmlWriting.WriteAttributeString("SenderID", "InkZone-Controller");
            XmlWriting.WriteAttributeString("Version", "1.2");
            //XmlWriting.WriteAttributeString("xmlns","",null, "http://www.CIP4.org/JDFSchema_1_1");

            XmlWriting.WriteStartElement("Command");
            XmlWriting.WriteAttributeString("ID", "cmd.00695");
            XmlWriting.WriteAttributeString("Type", "Resource");


            XmlWriting.WriteStartElement("ResourceCMDParams");
            XmlWriting.WriteAttributeString("ResourceName", "InkZoneProfile");
            XmlWriting.WriteAttributeString("JobID", "K_41");


            XmlWriting.WriteStartElement("InkZoneProfile");
            XmlWriting.WriteAttributeString("ID", "r0013");
            XmlWriting.WriteAttributeString("Class", "Parameter");
            XmlWriting.WriteAttributeString("Locked", "false");
            XmlWriting.WriteAttributeString("Status", "Available");
            XmlWriting.WriteAttributeString("PartIDKeys", "SignatureName SheetName Side Separation");
            XmlWriting.WriteAttributeString("DescriptiveName", "Schieberwerte von DI");
            XmlWriting.WriteAttributeString("ZoneWidth", "32");


            XmlWriting.WriteStartElement("InkZoneProfile");
            XmlWriting.WriteAttributeString("SignatureName", "SIG1");

            XmlWriting.WriteStartElement("InkZoneProfile");
            XmlWriting.WriteAttributeString("Locked", "False");
            XmlWriting.WriteAttributeString("SheetName", "S1");

            XmlWriting.WriteStartElement("InkZoneProfile");
            XmlWriting.WriteAttributeString("Side", "Front");

I've tried this too but didn't worked as expected(The while loop)

       while(XmlStuff.xml_filename == XmlItems.ItemsList[pos].xml_filename)
        {
            XmlWriting.WriteStartElement("InkZoneProfile");
            XmlWriting.WriteAttributeString("Separation", XmlItems.ItemsList[pos].colorname);
            XmlWriting.WriteAttributeString("ZoneSettingsX", XmlItems.ItemsList[pos].colorvalues);
            XmlWriting.WriteEndElement();
            pos++;
        }
    XmlWriting.WriteEndDocument();
    XmlWriting.Close();     
    }
}

Any help will be greately appreciated. Thanks a lot.

**

EDIT: XML Example

**.

<?xml version="1.0" encoding="utf-8"?>
<JMF SenderID="InkZone-Controller" Version="1.2" xmlns="http://www.CIP4.org/JDFSchema_1_1">
    <Command ID="cmd.00695" Type="Resource">
        <ResourceCmdParams ResourceName="InkZoneProfile" JobID="K_41">
            <InkZoneProfile ID="r0013" Class="Parameter" Locked="false" Status="Available" PartIDKeys="SignatureName SheetName Side Separation" DescriptiveName="Schieberwerte von DI" ZoneWidth="32">
                <InkZoneProfile SignatureName="SIG1">
                    <InkZoneProfile Locked="false" SheetName="S1">
                        <InkZoneProfile Side="Front">
                            <InkZoneProfile Separation="designer P&G 1901" ZoneSettingsX="0.391 0.36 0.097 0.058 0 0 0 0 0 0 0 0 0.178 0.394 0.201 0.088"/>
                            <InkZoneProfile Separation="designer P&G 0323" ZoneSettingsX="0.011 0.028 0.02 0.021 0.079 0.071 0.046 0.059 0.043 0.062 0.044 0.094 0.024 0.037 0.012 0.023"/>
                            <InkZoneProfile Separation="designer P&G 0068" ZoneSettingsX="0 0.04 0.03 0.016 0.08 0.005 0.041 0.036 0.034 0.044 0.028 0.072 0.001 0.005 0.063 0.008"/>
                            <InkZoneProfile Separation="designer P&G 0056" ZoneSettingsX="0 0.064 0.053 0.032 0.137 0.022 0.164 0.097 0.153 0.11 0.052 0.127 0.01 0.007 0.108 0.008"/>
                            <InkZoneProfile Separation="designer P&G 0306" ZoneSettingsX="0 0 0 0.19 0.187 0.259 0.224 0.178 0.24 0.185 0.249 0.188 0.173 0 0 0.004"/>
                            <InkZoneProfile Separation="designer P&G 0016" ZoneSettingsX="0.069 0.1 0.21 0.049 0.07 0.027 0.138 0.265 0.134 0.242 0.056 0.066 0.039 0.09 0.17 0.13"/>
                            <InkZoneProfile Separation="designer P&G 0057" ZoneSettingsX="0 0 0 0 0.023 0.001 0.012 0.012 0.008 0.016 0.003 0.021 0.001 0 0 0.004"/>
                            <InkZoneProfile Separation="Keyline" ZoneSettingsX="0.013 0.006 0.006 0.038 0.007 0.033 0.012 0.026 0.018 0.02 0.016 0.007 0.034 0.006 0.006 0.022"/>
                        </InkZoneProfile>
                    </InkZoneProfile>
                </InkZoneProfile>
            </InkZoneProfile>

This would be more accurate:

<InkZoneProfile Separation="Cyan" ZoneSettingsX="0.391 0.36 0.097 0.058 0 0 0 0 0 0 0 0 0.178 0.394 0.201 0.088"/>

Lets suppose this belongs to file1. File1 will have five SomeColor and SomeValues are in each position of the list.

解决方案

Is this what you want:

[Serializable]
[XmlType("InkZoneProfile")]
public class xmldata //Class to receive items list
{
    [XmlIgnore]
    public string xml_filename { get; set; }

    [XmlAttribute("Separation")]
    public string colorname { get; set; }

    [XmlAttribute("ZoneSettingsX")]
    public string colorvalues { get; set; }

}

FullList.Add(new xmldata { xml_filename = @"D:\File_One", colorname = "Red", colorvalues = "#FF0000" });
FullList.Add(new xmldata { xml_filename = @"D:\File_One", colorname = "Blue", colorvalues = "#0000FF" });
FullList.Add(new xmldata { xml_filename = @"D:\File_Two", colorname = "Black", colorvalues = "#000000" });
FullList.Add(new xmldata { xml_filename = @"D:\File_Two", colorname = "White", colorvalues = "#FFFFFF" });

Dictionary<string, List<xmldata>> xmlFiles = new Dictionary<string, List<xmldata>>();
foreach (var item in FullList)
{
    if (!xmlFiles.ContainsKey(item.xml_filename)) xmlFiles[item.xml_filename] = new List<xmldata>();
    xmlFiles[item.xml_filename].Add(item);
}

foreach (var i in xmlFiles)
{
    string yourXMLFileName = i.Key;
    List<xmldata> xmlDataOfThisFile = i.Value;
    Console.WriteLine(yourXMLFileName);
    try
    {
        using (FileStream fs = new FileStream(yourXMLFileName, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<xmldata>));
            serializer.Serialize(fs, FullList);
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.Message);
        Console.WriteLine(ex.InnerException);
    }
}

This will create this XML:

<?xml version="1.0"?>
<ArrayOfInkZoneProfile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <InkZoneProfile Separation="Red" ZoneSettingsX="#FF0000" />
  <InkZoneProfile Separation="Blue" ZoneSettingsX="#0000FF" />
  <InkZoneProfile Separation="Black" ZoneSettingsX="#000000" />
  <InkZoneProfile Separation="White" ZoneSettingsX="#FFFFFF" />
</ArrayOfInkZoneProfile>

EDIT

Based on your comment, you have an other problem. When the problem on the title question is solved now, you should ask new problem with other problem. Don't combine it here.

这篇关于从基于条件的列表中创建一个XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆