2017-02-24 7 views
1
**<Content> 
    <UId>1</UId> 
    <FileName>Calculator.txt</FileName> 
    <Image>1.jpg<Image/> 
    <FullPath>1</FullPath> 
    <FullPath>2</FullPath> 
    <FullPath>3</FullPath> 
    </Content>** 

上記のXMLのように出力します。そのために私は以下のコードを書いています。既存のXML文書にfullpathが存在する場合は追加しないでください。ただし、以下のコードを使用するとXMLが正しく記述されません。 2つのコンテンツノードが追加されます。XML要素ノードが2回追加されます

string fullPath="1$2$3"; 
List<string> nodesToBeAdded = fullPath.Split('$').ToList(); 

XmlDocument xmlDocument = new XmlDocument(); 
xmlDocument.Load(filePath); 

XmlNode nodeContent = xmlDocument.CreateNode(XmlNodeType.Element, "Content", null); 

XmlNode nodeUID = xmlDocument.CreateNode(XmlNodeType.Element, "UId", null); 
nodeUID.InnerText = value.UId; 

XmlNode nodeFileName = xmlDocument.CreateNode(XmlNodeType.Element, "FileName", null); 
nodeFileName.InnerText = value.FileName; 

XmlNode nodeImage = xmlDocument.CreateNode(XmlNodeType.Element, "Image", null); 
nodeImage.InnerText = value.Image; 

for (int i = 0; i < nodesToBeAdded.Count - 1; i++) 
{ 
    XmlNode nodeFullPath = xmlDocument.CreateNode(XmlNodeType.Element, "FullPath", null); 
    nodeFullPath.InnerText = nodesToBeAdded[i]; 

    if (xmlDocument.SelectNodes(string.Concat("//", "Content", '/', "FullPath", "[text()='" + nodesToBeAdded[i] + "']")).Count == 0) 
    { 
     nodeContent.AppendChild(nodeUID); 
     nodeContent.AppendChild(nodeFileName); 
     nodeContent.AppendChild(nodeImage); 
     nodeContent.AppendChild(nodeFullPath); 
    } 
}//add parent node to document 
xmlDocument.DocumentElement.AppendChild(nodeContent); 

xmlDocument.Save(filePath); 

出力:

<Content> 
    <UId>1</UId> 
    <FileName>Calculator.txt</FileName> 
    <Image>1.jpg</Image> 
    <FullPath>1</FullPath> 
    </Content> 
    <Content /> 
    <Content /> 
    <Content /> 
    <Content /> 
+1

LINQ to XMLを使用すると、はるかに簡単になります。 – Aybe

+0

^^参照:https://msdn.microsoft.com/en-us/library/mt693076.aspx – Fildor

答えて

2

私は、XMLを書くためにあなたのルールについてはよく分からないが、私はあなたが正しい、同じ内容に複数のコンテンツとフルパスカントの繰り返しを記述する必要が見ることができるよう?だから、

、あなたは命令

if (xmlDocument.SelectNodes(string.Concat("//", "Content", '/', "FullPath", "[text()='" + nodesToBeAdded[i] + "']")).Count == 0) 
{ 
    nodeContent.AppendChild(nodeUID); 
    nodeContent.AppendChild(nodeFileName); 
    nodeContent.AppendChild(nodeImage); 
    nodeContent.AppendChild(nodeFullPath); 
} 

を使用するときに任意のを探していますので、あなたのコード内の文は、空の<コンテンツ/ >タグを生成するための責任がある場合、私はを見ることができるようfullpathにはtext = nodesToBeAdded [i]があり、最初のループの後で条件if (xmlDocument.SelectNodes(string.Concat("//", "Content", '/', "FullPath", "[text()='" + nodesToBeAdded[i] + "']")).Count == 0)は常にtrueを返し、その結果、ノード上のすべての要素を追加するコードは実行されません。

私はデ現在の内容を確認できます、あなたのコード内のいくつかの修正は、以下を参照してくださいでした:

static void Main(string[] args) 
{ 
     string filePath = "D:\\teste.xml"; 
     string fullPath = "1$2$3"; 
     List<string> nodesToBeAdded = fullPath.Split('$').ToList(); 

     XmlDocument xmlDocument = new XmlDocument(); 

     xmlDocument.Load(filePath); 

     for (int item = 1; item <= 3; item++) 
     { 
      XmlNode nodeContent = xmlDocument.CreateNode(XmlNodeType.Element, "Content", null); 

      XmlNode nodeUID = xmlDocument.CreateNode(XmlNodeType.Element, "UId", null); 
      nodeUID.InnerText = item.ToString();//value.UId; 

      XmlNode nodeFileName = xmlDocument.CreateNode(XmlNodeType.Element, "FileName", null); 
      nodeFileName.InnerText = item + "-Calculator.txt";// value.FileName; 

      XmlNode nodeImage = xmlDocument.CreateNode(XmlNodeType.Element, "Image", null); 
      nodeImage.InnerText = item + "-Image.jpg";//value.Image; 

      for (int i = 0; i < nodesToBeAdded.Count; i++) 
      { 
       XmlNode nodeFullPath = xmlDocument.CreateNode(XmlNodeType.Element, "FullPath", null); 
       nodeFullPath.InnerText = nodesToBeAdded[i]; 

       if (nodeContent.SelectNodes(string.Concat("//", "Content", '/', "FullPath", "[text()='" + nodesToBeAdded[i] + "']")).Count == 0) 
       { 
        nodeContent.AppendChild(nodeUID); 
        nodeContent.AppendChild(nodeFileName); 
        nodeContent.AppendChild(nodeImage); 
        nodeContent.AppendChild(nodeFullPath); 
       } 
      }//add parent node to document 
      xmlDocument.DocumentElement.AppendChild(nodeContent); 
     } 
     xmlDocument.Save("D:\\teste.xml"); 
    } 

と結果

"<?xml version="1.0" encoding="utf-8"?> 
    <root> 
     <Content> 
     <FullPath>1</FullPath> 
     <FullPath>2</FullPath> 
     <UId>1</UId> 
     <FileName>1-Calculator.txt</FileName> 
     <Image>1-Image.jpg</Image> 
     <FullPath>3</FullPath> 
     </Content> 
     <Content> 
     <FullPath>1</FullPath> 
     <FullPath>2</FullPath> 
     <UId>2</UId> 
     <FileName>2-Calculator.txt</FileName> 
     <Image>2-Image.jpg</Image> 
     <FullPath>3</FullPath> 
     </Content> 
     <Content> 
     <FullPath>1</FullPath> 
     <FullPath>2</FullPath> 
     <UId>3</UId> 
     <FileName>3-Calculator.txt</FileName> 
     <Image>3-Image.jpg</Image> 
     <FullPath>3</FullPath> 
     </Content> 
    </root>" 

私はそれはあなたを助けることができると思います。

0

と仮定すると、あなたのXMLは次のようになります。

<root> 
    <Content> 
     <UId>1</UId> 
     <FileName>Calculator.txt</FileName> 
     <Image>1.jpg</Image> 
     <FullPath>1</FullPath> 
     <FullPath>2</FullPath> 
     <FullPath>3</FullPath> 
    </Content> 
    <Content> 
     <UId>2</UId> 
     <FileName>Calculator2.txt</FileName> 
     <Image>1.jpg</Image> 
     <FullPath>1</FullPath> 
     <FullPath>2</FullPath> 
     <FullPath>3</FullPath> 
    </Content> 
</root> 

あなたが読んで、XMLを生成することができ、このhttp://xmltocsharp.azurewebsites.net/

/* 
    Licensed under the Apache License, Version 2.0 

    http://www.apache.org/licenses/LICENSE-2.0 
    */ 
using System; 
using System.Xml.Serialization; 
using System.Collections.Generic; 
namespace Xml2CSharp 
{ 
    [XmlRoot(ElementName="Content")] 
    public class Content { 
     [XmlElement(ElementName="UId")] 
     public string UId { get; set; } 
     [XmlElement(ElementName="FileName")] 
     public string FileName { get; set; } 
     [XmlElement(ElementName="Image")] 
     public string Image { get; set; } 
     [XmlElement(ElementName="FullPath")] 
     public List<string> FullPath { get; set; } 
    } 

    [XmlRoot(ElementName="root")] 
    public class Root { 
     [XmlElement(ElementName="Content")] 
     public List<Content> Content { get; set; } 
    } 

} 

を使用することができます:

あなたがこれを使用することができます
// READ 
var data = System.IO.File.ReadAllText(@"C:\temp\file.xml"); 
Root root; 

var serializer = new XmlSerializer(typeof(Root)); 

using (var stream = new StringReader(data)) 
using (var reader = XmlReader.Create(stream)) 
{ 
    root = (Root)serializer.Deserialize(reader); 
} 

// WORK ON YOUR OBJECT. 
// TODO ... 

// WRITE 
//var root = new Root(); 
root.Content = new List<Content>(); 
root.Content.Add(new Content { UId = "1", FileName = "file1.jpg" }); 
root.Content.Add(new Content { UId = "2", FileName = "file2.jpg" }); 

//var serializer = new System.Xml.Serialization.XmlSerializer(typeof(Root)); 
serializer.Serialize(File.Create(@"C:\temp\file1.xml"), root); 

OR

Visual Studio -> Edit -> Paste as XML

WICHは、このコードを生成します。

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)] 
public partial class root 
{ 

    private rootContent[] contentField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("Content")] 
    public rootContent[] Content 
    { 
     get 
     { 
      return this.contentField; 
     } 
     set 
     { 
      this.contentField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class rootContent 
{ 

    private byte uIdField; 

    private string fileNameField; 

    private string imageField; 

    private byte[] fullPathField; 

    /// <remarks/> 
    public byte UId 
    { 
     get 
     { 
      return this.uIdField; 
     } 
     set 
     { 
      this.uIdField = value; 
     } 
    } 

    /// <remarks/> 
    public string FileName 
    { 
     get 
     { 
      return this.fileNameField; 
     } 
     set 
     { 
      this.fileNameField = value; 
     } 
    } 

    /// <remarks/> 
    public string Image 
    { 
     get 
     { 
      return this.imageField; 
     } 
     set 
     { 
      this.imageField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("FullPath")] 
    public byte[] FullPath 
    { 
     get 
     { 
      return this.fullPathField; 
     } 
     set 
     { 
      this.fullPathField = value; 
     } 
    } 
} 

次に、あなたが、例えば、XMLファイルとして保存することができます

var r = new root(); 
    r.Content = new rootContent[2]; 
    r.Content[0] = new rootContent() { UId = 1, FileName = "file1.jpg" }; 
    r.Content[1] = new rootContent() { UId = 2, FileName = "file2.jpg" }; 


    var serializer = new System.Xml.Serialization.XmlSerializer(typeof(root)); 
    serializer.Serialize(File.Create(@"C:\temp\file.xml"), r); 
0

私が正しくあなたの状況を理解している場合、ifブロックにxmlDocument.DocumentElement.AppendChild(nodeContent);を移動させることに役立つはずです。

string fullPath="1$2$3"; 
List<string> nodesToBeAdded = fullPath.Split('$').ToList(); 

XmlDocument xmlDocument = new XmlDocument(); 
xmlDocument.Load(filePath); 

XmlNode nodeContent = xmlDocument.CreateNode(XmlNodeType.Element, "Content", null); 

XmlNode nodeUID = xmlDocument.CreateNode(XmlNodeType.Element, "UId", null); 
nodeUID.InnerText = value.UId; 

XmlNode nodeFileName = xmlDocument.CreateNode(XmlNodeType.Element, "FileName", null); 
nodeFileName.InnerText = value.FileName; 

XmlNode nodeImage = xmlDocument.CreateNode(XmlNodeType.Element, "Image", null); 
nodeImage.InnerText = value.Image; 

bool addNodeContent = false; 
for (int i = 0; i < nodesToBeAdded.Count - 1; i++) 
{ 
    XmlNode nodeFullPath = xmlDocument.CreateNode(XmlNodeType.Element, "FullPath", null); 
    nodeFullPath.InnerText = nodesToBeAdded[i]; 

    if (xmlDocument.SelectNodes(string.Concat("//", "Content", '/', "FullPath", "[text()='" + nodesToBeAdded[i] + "']")).Count == 0) 
    { 
     nodeContent.AppendChild(nodeUID); 
     nodeContent.AppendChild(nodeFileName); 
     nodeContent.AppendChild(nodeImage); 
     nodeContent.AppendChild(nodeFullPath); 

     addNodeContent = true; 
    } 
} 

if (addNodeContent) 
{ 
    //add parent node to document 
    xmlDocument.DocumentElement.AppendChild(nodeContent); 
} 

xmlDocument.Save(filePath); 

そうでない場合は、空のノードが追加されます。しかし、私はあなたのコードのいくつかの瞬間についてはまだ分かりませんので、C#で提供される自動XMLシリアル化ツールの使用を検討してください。

関連する問題