2017-04-30 8 views
1

지옥의 모든 속성XML을 쓰는 여러 메신저 판매 더 이상 나열 없지만 unfortunetly 나는 이것이 orginal 한 XML 파일 무언가를 밖으로</p> <p>를 그리워하는 것 항목없이 새로운 XML 파일에 현재 XML 파일을 다시 작성하려고

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<imgdir name="Commodity.img"> 
    <imgdir name="0"> 
     <int name="SN" value="10000000"/> 
     <int name="ItemId" value="1002000"/> 
     <int name="Count" value="1"/> 
     <int name="Price" value="300"/> 
     <int name="Period" value="14"/> 
     <int name="Priority" value="8"/> 
     <int name="Gender" value="2"/> 
     <int name="OnSale" value="0"/> 
    </imgdir> 
    <imgdir name="1"> 
     <int name="SN" value="10000001"/> 
     <int name="ItemId" value="1002015"/> 
     <int name="Count" value="1"/> 
     <int name="Price" value="300"/> 
     <int name="Period" value="14"/> 
     <int name="Priority" value="8"/> 
     <int name="Gender" value="2"/> 
     <int name="OnSale" value="0"/> 
    </imgdir> 
    <imgdir name="2"> 
     <int name="SN" value="10000002"/> 
     <int name="ItemId" value="1002187"/> 
     <int name="Count" value="1"/> 
     <int name="Price" value="300"/> 
     <int name="Period" value="14"/> 
     <int name="Priority" value="8"/> 
     <int name="Gender" value="2"/> 
     <int name="OnSale" value="0"/> 
    </imgdir> 
    <imgdir name="3"> 
     <int name="SN" value="10000003"/> 
     <int name="ItemId" value="1002292"/> 
     <int name="Count" value="1"/> 
     <int name="Price" value="300"/> 
     <int name="Period" value="14"/> 
     <int name="Priority" value="8"/> 
     <int name="Gender" value="2"/> 
     <int name="OnSale" value="0"/> 
    </imgdir> 

이 내가 내 코드에서 얻을 출력이 잘못하고

<?xml version="1.0" encoding="utf-8"?><imgdir xmlns="Commodity.img"> 
<imgdir SN="10002304" ItemId="5010073" Count="1" Price="3300" Period="90" Priority="12" Gender="2" OnSale="1" xmlns="2" /> 
<imgdir SN="10002305" ItemId="5010074" Count="1" Price="3300" Period="90" Priority="12" Gender="2" OnSale="1" xmlns="3" /> 
<imgdir SN="10002306" ItemId="1022104" Count="1" Price="3300" Period="90" Priority="12" Gender="2" OnSale="1" xmlns="4" /> 
<imgdir SN="10002307" ItemId="9102232" Count="1" Price="16900" Period="90" Priority="12" Gender="2" OnSale="1" xmlns="5" /> 
<imgdir SN="10002308" ItemId="1003050" Count="1" Price="2500" Period="90" Priority="12" Gender="2" OnSale="1" xmlns="6" /> 
<imgdir SN="10002309" ItemId="1022102" Count="1" Price="3200" Period="90" Priority="12" Gender="2" OnSale="1" xmlns="7" /> 
</imgdir> 

어떤 제안 무엇을 임? 내 출력

XmlWriter writer = XmlWriter.Create("NewXml.xml"); 
      int number = 0; 
      string OLD_itemSN = "0"; 
      writer.WriteStartElement("imgdir", "Commodity.img"); 
      foreach (Cashitem item in Cashitems) 
      { 

       if (item.OnSale != 0) 
       { 
        if (item.SN.ToString() != OLD_itemSN) 
        { 
         number++; 
         OLD_itemSN = item.SN.ToString(); 
        } 
        writer.WriteStartElement("imgdir", number.ToString()); 
        writer.WriteAttributeString("SN", item.SN.ToString()); 
        writer.WriteAttributeString("ItemId", item.ItemId.ToString()); 
        writer.WriteAttributeString("Count", item.Count.ToString()); 
        writer.WriteAttributeString("Price", item.Price.ToString()); 
        writer.WriteAttributeString("Period", item.Period.ToString()); 
        writer.WriteAttributeString("Priority", item.Priority.ToString()); 
        writer.WriteAttributeString("Gender", item.Gender.ToString()); 
        writer.WriteAttributeString("OnSale", item.OnSale.ToString()); 
        if (item.Limit != null) 
        { 
         writer.WriteAttributeString("Limit", item.Limit); 
        } 
        writer.WriteEndElement(); 
       } 
      } 
      writer.WriteEndElement(); 
      writer.Flush(); 


     } 

나는 imgdir 이름 = 값 를 그리워하고 모든 속성 메신저 정말 desperated 전에 :(

+0

C# 언어입니까? –

답변

0

혼동하지 마십시오 속성요소를!

이 시도 int로 코드 :

var settings = new XmlWriterSettings { Indent = true }; 
using (XmlWriter writer = XmlWriter.Create("NewXml.xml", settings)) 
{ 
    writer.WriteStartDocument(standalone: true); 

    int number = 0; 
    string OLD_itemSN = "0"; 
    writer.WriteStartElement("imgdir"); 
    writer.WriteAttributeString("name", "Commodity.img"); 

    foreach (Cashitem item in Cashitems) 
    { 
     if (item.OnSale != 0) 
     { 
      if (item.SN.ToString() != OLD_itemSN) 
      { 
       number++; 
       OLD_itemSN = item.SN.ToString(); 
      } 
      writer.WriteStartElement("imgdir"); 
      writer.WriteAttributeString("name", number.ToString()); 

      WriteIntNode(writer, "SN", item.SN.ToString()); 
      WriteIntNode(writer, "ItemId", item.ItemId.ToString()); 
      WriteIntNode(writer, "Count", item.Count.ToString()); 
      WriteIntNode(writer, "Price", item.Price.ToString()); 
      WriteIntNode(writer, "Period", item.Period.ToString()); 
      WriteIntNode(writer, "Priority", item.Priority.ToString()); 
      WriteIntNode(writer, "Gender", item.Gender.ToString()); 
      WriteIntNode(writer, "OnSale", item.OnSale.ToString()); 

      if (item.Limit != null) 
      { 
       writer.WriteAttributeString("Limit", item.Limit); 
      } 
      writer.WriteEndElement(); 
     } 
    } 
} 

광고 Ditional 방식 :

void WriteIntNode(XmlWriter writer, string name, string value) 
{ 
    writer.WriteStartElement("int"); 
    writer.WriteAttributeString("name", name); 
    writer.WriteAttributeString("value", value); 
    writer.WriteEndElement(); 
}