我尝试读取从log4net UdpAppender捕获的以下字符串.
<log4net:event logger="TestingTransmitter.Program"
timestamp="2009-08-02T17:50:18.928+01:00"
level="ERROR"
thread="9"
domain="TestingTransmitter.vshost.exe"
username="domain\user">
<log4net:message>Log entry 103</log4net:message>
<log4net:properties>
<log4net:data name="log4net:HostName" value="machine" />
</log4net:properties>
</log4net:event>
Run Code Online (Sandbox Code Playgroud)
在尝试XElement.Parse或XDocument.Parse内容时,它会抛出异常:
'log4net'是未声明的命名空间.第1行,第2位.
我知道我可以在原始字符串中搜索并替换"log4net:"并删除它,允许我成功解析XML,但是有更好的方法吗?这是捕获的完整数据(重新格式化以允许读取),没有制作或删除xml命名空间声明.
我在c#中有一个三个List,变量名是l_lstData1, l_lstData2, l_lstData3.
文件结构是
<FileDetails>
<Date FileModified="29/04/2010 12:34:02" />
<Data Name="Data_1" DataList="India" Level="2" />
<Data Name="Data_2" DataList="chennai" Level="2" />
<Data Name="Data_3" DataList="hyderabad" Level="2" />
<Data Name="Data_4" DataList="calcutta" Level="2" />
<Data Name="Data_5" DataList="vijayawada" Level="1" />
<Data Name="Data_6" DataList="cochin" Level="1" />
<Data Name="Data_7" DataList="madurai" Level="0" />
<Data Name="Data_8" DataList="trichy" Level="0" />
</FileDetails>
Run Code Online (Sandbox Code Playgroud)
3个列表的值如下:
l_lstData1[0] = "India";
l_lstData1[1] = "chennai";
l_lstData1[2] = "hyderabad";
l_lstData1[3] = "calcutta";
Run Code Online (Sandbox Code Playgroud)
所以上面的XML(element:Data)的level属性值="2".
l_lstData2[0] = "vijayawada";
l_lstData2[1] = "cochin";
Run Code Online (Sandbox Code Playgroud)
所以上面的XML(element:Data)的level属性值="1".
l_lstData3[0] = "madurai";
l_lstData3[1] = "trichy";
Run Code Online (Sandbox Code Playgroud)
所以上面的XML(element:Data)的level属性值为"0".
我有一个用C#编写的Windows桌面应用程序,它循环存储在磁盘上并由第三方程序创建的一堆XML文件.大多数所有文件都由此语句后面的LINQ代码成功加载和处理:
XDocument xmlDoc = XDocument.Load(inFileName);
List<DocMetaData> docList =
(from d in xmlDoc.Descendants("DOCUMENT")
select new DocMetaData
{
File = d.Element("FILE").SafeGetAttributeValue("filename")
,
Folder = d.Element("FOLDER").SafeGetAttributeValue("name")
,
ItemID = d.Elements("INDEX")
.Where(i => (string)i.Attribute("name") == "Item ID(idmId)")
.Select(i => (string)i.Attribute("value"))
.FirstOrDefault()
,
Comment = d.Elements("INDEX")
.Where(i => (string)i.Attribute("name") == "Comment(idmComment)")
.Select(i => (string)i.Attribute("value"))
.FirstOrDefault()
,
Title = d.Elements("INDEX")
.Where(i => (string)i.Attribute("name") == "Title(idmName)")
.Select(i => (string)i.Attribute("value"))
.FirstOrDefault()
,
DocClass = d.Elements("INDEX")
.Where(i => (string)i.Attribute("name") == "Document Class(idmDocType)")
.Select(i => (string)i.Attribute("value"))
.FirstOrDefault()
}
).ToList<DocMetaData>();
Run Code Online (Sandbox Code Playgroud)
...其中inFileName是一个完整的路径和文件名,例如: …
如何IncomingConfig使用linq to xml 检查元素是否存在?
<?xml version="1.0" encoding="utf-8"?>
<settings>
<IncomingConfig>
<ip>10.100.101.18</ip>
<port>5060</port>
</IncomingConfig>
<Device>
<username>tarek</username>
<AgentName>tarek</AgentName>
<password>ffff</password>
</Device>
<Device>
<username>adf</username>
<AgentName>adf</AgentName>
<password>fadsf</password>
</Device>
</settings>
Run Code Online (Sandbox Code Playgroud) 我已经创建了一个方法来检查XML文件中是否存在属性.如果它不存在则返回"False".它可以工作,但解析文件需要很长时间.它似乎读取每一行的整个文件.我错过了什么吗?我可以以某种方式使它更有效吗?
public static IEnumerable<RowData> getXML(string XMLpath)
{
XDocument xmlDoc = XDocument.Load("spec.xml");
var specs = from spec in xmlDoc.Descendants("spec")
select new RowData
{
number= (string)spec.Attribute("nbr"),
name= (string)spec.Attribute("name").Value,
code = (string)spec.Attribute("code").Value,
descr = (string)spec.Attribute("descr").Value,
countObject = checkXMLcount(spec),
return specs;
}
public static string checkXMLcount(XElement x)
{
Console.WriteLine(x.Attribute("nbr").Value);
Console.ReadLine();
try
{
if (x.Attribute("mep_count").Value == null)
{
return "False";
}
else
{
return x.Attribute("mep_count").Value;
}
}
catch
{
return "False";
}
}
Run Code Online (Sandbox Code Playgroud)
我测试用一个只返回和接收字符串的方法替换方法:
public static string checkXMLcount(string x)
{
Console.WriteLine(x);
Console.ReadLine();
return x;
} …Run Code Online (Sandbox Code Playgroud) 我试图将XDocument的默认缩进从2更改为3,但我不太确定如何继续.如何才能做到这一点?
我熟悉XmlTextWriter并使用过代码:
using System.Xml;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
string destinationFile = "C:\myPath\results.xml";
XmlTextWriter writer = new XmlTextWriter(destinationFile, null);
writer.Indentation = 3;
writer.WriteStartDocument();
// Add elements, etc
writer.WriteEndDocument();
writer.Close();
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于我使用的另一个项目,XDocument因为它对我的实现更有效,类似于:
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Xml;
using System.Text;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
// Source file has indentation of 3
string sourceFile = @"C:\myPath\source.xml";
string destinationFile = @"C:\myPath\results.xml";
List<XElement> …Run Code Online (Sandbox Code Playgroud) 我有一个XElement看起来像这样:
<row flag="1" sect="" header="" body="" extrainfo="0" />
Run Code Online (Sandbox Code Playgroud)
然后我有一个看起来像这样的课:
public class ProductAttribute
{
public string Flag { get; set; }
public string Sect { get; set; }
public string Header { get; set; }
public string Body { get; set; }
public string Extrainfo { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如何将其XElement转换为ProductAttribute对象?
我有一个XDocument,我想按字母顺序对所有元素进行排序.这是结构的简化版本:
<Config>
<Server>
<Id>svr1</Id>
<Routing>
<RoutingNodeName>route1</RoutingNodeName>
<Subscription>
<Id>1</Id>
</Subscription>
<RoutingParameters id="Routing1">
<Timeout>7200</Timeout>
</RoutingParameters>
</Routing>
<Storage>
<Physical>HD1</Physical>
</Storage>
</Server>
<Applications>
<Services>
<Local></Local>
</Services>
</Applications>
</Config>
Run Code Online (Sandbox Code Playgroud)
我想在各个级别对这些文档中的元素进行排序,到目前为止我能够像这样对它进行排序:
private static XDocument Sort(XDocument file)
{
return new XDocument(
new XElement(file.Root.Name,
from el in file.Root.Elements()
orderby el.Name.ToString()
select el));
}
Run Code Online (Sandbox Code Playgroud)
哪个产生:
<Config>
<Applications>
<Services>
<Local></Local>
</Services>
</Applications>
<Server>
<Id>svr1</Id>
<Routing>
<RoutingNodeName>route1</RoutingNodeName>
<Subscription>
<Id>1</Id>
</Subscription>
<RoutingParameters id="Routing1">
<Timeout>7200</Timeout>
</RoutingParameters>
</Routing>
<Storage>
<Physical>HD1</Physical>
</Storage>
</Server>
</Config>
Run Code Online (Sandbox Code Playgroud)
我希望能够以相同的方式对所有子元素进行排序(理想情况下通过递归函数).任何想法我怎么能用LINQ得到这个?
谢谢你的任何想法.
我有一个iPhone应用程序的plist文件.它如下所示:
<plist version="1.0">
<dict>
<key>barcodes</key>
<array>
<string>JF893J89FJ-66666</string>
<string>JF893J89FJ-55555</string>
</array>
<key>currentStep</key>
<integer>1</integer>
<key>dateFinished</key>
<date>2010-05-10T18:33:25Z</date>
<key>dateStarted</key>
<date>2010-05-10T18:33:25Z</date>
<key>description</key>
<string>TEST</string>
<key>geoRequired</key>
<string>N</string>
<key>inProgress</key>
<string>N</string>
<key>jobID</key>
<integer>10085</integer>
<key>jobSteps</key>
<array>
<dict>
<key>label</key>
<string>TEST</string>
<key>response</key>
<string>matt hudson</string>
<key>stepID</key>
<integer>1103</integer>
<key>typeID</key>
<integer>4</integer>
</dict>
</array>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)
我需要在jobSteps之后获取数组.
到目前为止我有这个:
XDocument xml = XDocument.Load(rri.Response);
var q = from elements in xml.Descendants("plist").Descendants("dict")
where elements.Value == "jobSteps"
select elements;
Run Code Online (Sandbox Code Playgroud)
但我需要在其中包含jobSteps的元素之后获取下一个项目.
这与我的C#Generic List转换为实现List <T>的类的先前问题有关
我有以下代码:
public abstract class DataField
{
public string Name { get; set; }
}
public class DataField<T> : DataField
{
public T Value { get; set; }
}
public static List<DataField> ConvertXML(XMLDocument data) {
result = (from d in XDocument.Parse(data.OuterXML).Root.Decendendants()
select new DataField<string>
{
Name = d.Name.ToString(),
Value = d.Value
}).Cast<DataField>().ToList();
return result;
}
Run Code Online (Sandbox Code Playgroud)
这工作但我希望能够修改LINQ查询的选择部分是这样的:
select new DataField<[type defined in attribute of XML Element]>
{
Name = d.Name.ToString(),
Value = d.Value
}
Run Code Online (Sandbox Code Playgroud)
这只是一种糟糕的方法吗?可能吗?有什么建议?