C# Xml元素节点被追加两次 ** 1. 计算器.txt 1.jpg 1. 2. 3. **
我想要像上面的XML一样的输出。为此,我编写了下面的代码。如果现有XML文档中存在完整路径,则不要添加它,但使用XML下面的代码是错误的。添加了两个内容节点C# Xml元素节点被追加两次 ** 1. 计算器.txt 1.jpg 1. 2. 3. **,c#,.net,xml,C#,.net,Xml,我想要像上面的XML一样的输出。为此,我编写了下面的代码。如果现有XML文档中存在完整路径,则不要添加它,但使用XML下面的代码是错误的。添加了两个内容节点 **<Content> <UId>1</UId> <FileName>Calculator.txt</FileName> <Image>1.jpg<Image/> <FullPath>1</FullPath
**<Content>
<UId>1</UId>
<FileName>Calculator.txt</FileName>
<Image>1.jpg<Image/>
<FullPath>1</FullPath>
<FullPath>2</FullPath>
<FullPath>3</FullPath>
</Content>**
string fullPath=“1$2$3”;
List nodesToBeAdded=fullPath.Split(“$”).ToList();
XmlDocument XmlDocument=新的XmlDocument();
加载(文件路径);
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,“文件名”,null);
nodeFileName.InnerText=value.FileName;
XmlNode nodeImage=xmlDocument.CreateNode(XmlNodeType.Element,“Image”,null);
nodeImage.InnerText=value.Image;
for(int i=0;i
输出:
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);
1.
计算器.txt
1.jpg
1.
假设您的XML如下所示:
<Content>
<UId>1</UId>
<FileName>Calculator.txt</FileName>
<Image>1.jpg</Image>
<FullPath>1</FullPath>
</Content>
<Content />
<Content />
<Content />
<Content />
如果我正确理解了您的情况,请移动
xmlDocument.DocumentElement.AppendChild(nodeContent)代码>到if
块应有助于:
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);
string fullPath=“1$2$3”;
List nodesToBeAdded=fullPath.Split(“$”).ToList();
XmlDocument XmlDocument=新的XmlDocument();
加载(文件路径);
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,“文件名”,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
否则,将添加空节点。但是我仍然不确定代码中的一些时刻,所以请考虑使用C语言中提供的自动XML序列化工具。 < P>我不确定你写XML的规则,但是我可以看到你必须写多个内容,并且<强>全文路径>不能在<强>相同的内容< /强>中重复,对吗?
因此,正如我在代码中看到的if语句负责生成空的标记,因为当您使用指令时
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);
您将查找具有text=nodestobeaded[i]的任何完整路径,在第一个循环之后,您的条件if(xmlDocument.SelectNodes(string.Concat(“/”、“Content”、“/”、“/”、“fullpath”、“[text()=”+nodestobeaded[i]+“])。计数==0)
将始终返回true,因此无法执行可用于添加节点上所有元素的代码
我在您的代码中做了一些修复,允许检查当前内容,请参见以下内容:
if (xmlDocument.SelectNodes(string.Concat("//", "Content", '/', "FullPath", "[text()='" + nodesToBeAdded[i] + "']")).Count == 0)
{
nodeContent.AppendChild(nodeUID);
nodeContent.AppendChild(nodeFileName);
nodeContent.AppendChild(nodeImage);
nodeContent.AppendChild(nodeFullPath);
}
static void Main(字符串[]args)
{
string filePath=“D:\\teste.xml”;
字符串fullPath=“1$2$3”;
List nodesToBeAdded=fullPath.Split(“$”).ToList();
XmlDocument XmlDocument=新的XmlDocument();
加载(文件路径);
对于(int item=1;item使用LINQ到XML)将更容易。^^^请参阅:
/// <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;
}
}
}
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);
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);
if (xmlDocument.SelectNodes(string.Concat("//", "Content", '/', "FullPath", "[text()='" + nodesToBeAdded[i] + "']")).Count == 0)
{
nodeContent.AppendChild(nodeUID);
nodeContent.AppendChild(nodeFileName);
nodeContent.AppendChild(nodeImage);
nodeContent.AppendChild(nodeFullPath);
}
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>"