根据 MSDN:
defattr
类型:System.Boolean
如果true ,则从XmlReader复制默认属性;否则为 false。如果为true,则使用默认属性;否则为假。
我的问题是作者这样说是什么意思?
我有以下XML代码段 -
-<Row>
<RowType Id="1"Label="Scotland">1985</RowType>
<Year Id="11"Label="1994"/>
<Value Id="123">18</Value>
<Field Id="123"Label="Country">16</Field>
<Field Id="123"Label="Soccer">Yes</Field>
</Row>
-<Row>
<RowType Id="1"Label="England">1986</RowType>
<Year Id="11"Label="1994"/>
<Value Id="123">19</Value>
<Field Id="123"Label="Country">16</Field>
<Field Id="123"Label="Soccer">Yes</Field>
</Row>
-<Row>
<RowType Id="1"Label="Wales">1987</RowType>
<Year Id="11"Label="1994"/>
<Value Id="123">20</Value>
<Field Id="123"Label="Country">16</Field>
<Field Id="123"Label="Soccer">Yes</Field>
</Row>
Run Code Online (Sandbox Code Playgroud)
我正在使用它XmlReader从中检索特定数据 -
using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
{
string country = "";
string Year = "";
string count = "";
string tss= "";
string tss2 = "";
reader.MoveToContent();
while (reader.Read())
{
reader.ReadToFollowing("RowType");
country = reader.GetAttribute("Label");
country = country.Replace("'", ""); …Run Code Online (Sandbox Code Playgroud) 好的,我在学习 XmlSerializer 时遇到了一些障碍。我遵循了所有推荐的步骤,但我的程序没有返回任何内容,或者返回 null。我创建了一个 XML 文件,如下所示:
<?xml version='1.0' encoding='UTF-8' ?>
<WeeklyJobs>
<DailyJobs Date = "02/03/2012"/>
<DailyJobs Date = "02/04/2012" TotalJobs = "2">
<Jobs>
<Job JobName = "Job Name" Description = "Description"/>
<Job JobName = "Job Name" Description = "Description"/>
</Jobs>
</DailyJobs>
<DailyJobs Date = "02/05/2012" TotalJobs = "1">
<Jobs>
<Job JobName = "Job Name" Description = "Description"/>
</Jobs>
</DailyJobs>
<DailyJobs Date = "02/06/2012" TotalJobs = "2">
<Jobs>
<Job JobName = "Job Name" Description = "Description"/>
<Job JobName = "Job Name" …Run Code Online (Sandbox Code Playgroud) 假设我有以下 Xml:
<Sections>
<Section>
<Item>
<Field> myfield </Field>
<Field> myfield </Field>
</Item>
<Item>
<Field> myfield </Field>
<Field> myfield </Field>
</Item>
</Section>
<Section>
<Item>
<Field> myfield </Field>
<Field> myfield </Field>
</Item>
</Section>
</Sections>
Run Code Online (Sandbox Code Playgroud)
现在我想要的是循环遍历各个部分,并分别处理每个项目,所以我正在考虑执行如下操作:
reader.ReadToDescendant("Section")
do
{
Console.WriteLine("Section");
reader.ReadToDescendant("Item");
do
{
var element = (XElement)XNode.ReadFrom(reader);
foreach (XElement el in element.Elements())
{
Console.WriteLine(el.Value);
}
}while(reader.ReadToNextSibling("Item"));
}while (reader.ReadToNextSibling("Section"))
Run Code Online (Sandbox Code Playgroud)
我的问题是。如果我对 Item 节点重复相同的 do-while 循环,那么读取器在找到结束部分标记时是否会停止,或者它将在所有 xml 中进行搜索?我应该在内部循环之前使用 reader.ReadSubtree() 吗?
请注意,我并不是在寻找“使用 XDocument”之类的标准答案。我知道 dom 更容易使用,但它们不适合我的情况
我正在使用XmlReader迭代一些XML.一些XML实际上是HTML,我想从节点获取文本内容.
示例XML:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<p>Here is some <b>data</b></p>
</data>
Run Code Online (Sandbox Code Playgroud)
示例代码:
using (XmlReader reader = new XmlReader(myUrl))
{
while (reader.Read())
{
if (reader.Name == "p")
{
// I want to get all the TEXT contents from the this node
myVar = reader.Value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这不能得到我所有的内容.我如何从中获取所有内容
节点在那种情况下?
我正在尝试读取xml文档,但XmlReader.ReadToNextSibling不能像在此MSDN文档中所宣传的那样工作
这是一个控制台示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
namespace ConsoleTestApp
{
class Program
{
static void Main(string[] args)
{
string xmlText = "<xmlRoot><group><item>Item 1</item><item>Item 2</item></group><group><item>Item 3</item><item>Item 4</item></group></xmlRoot>";
using (XmlReader reader = XmlReader.Create(new StringReader(xmlText)))
{
reader.ReadToFollowing("item");
do
{
Console.WriteLine("Item: {0}", reader.ReadInnerXml());
} while (reader.ReadToNextSibling("item"));
}
Console.WriteLine("Press any key to continue...");
Console.Read();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这只输出一个项目:
货号:第1项
谁知道我怎么能让这个工作?
请不要建议使用DOM模型(XmlDocument).我不能,因为这些xml文件来自不同的来源,并且可以有许多不同的命名空间,这是一个巨大的麻烦.我需要让这个工作.
我有个问题.
我正在使用XMLReader类来获取a NSDictionary并且一切正常.但我无法获取productData元素的属性值.
具体来说,我有以下内容NSDictionary:
{
response = {
products = {
productsData = (
{
alias = "Product 1";
id = 01;
price = "10";
},
{
alias = "Product 2";
id = 02;
price = "20";
},
{
alias = "Product 3";
id = 03;
price = "30";
});
};
};
}
Run Code Online (Sandbox Code Playgroud)
我用这段代码来创建de NSDictionary:
NSDictionary *dictionary = [XMLReader dictionaryForXMLData:responseData error:&parseError];
Run Code Online (Sandbox Code Playgroud)
和responseData包含:
<application>
<products>
<productData>
<id>01</id>
<price>10</price>
<alias>Product 1</alias>
</productData>
<productData>
<id>02</id>
<price>20</price>
<alias>Product 2</alias>
</productData> …Run Code Online (Sandbox Code Playgroud) 我已经构建了一个非常简单的表,显示4列和4行.执行以下代码时,它将显示.xml文件中的每个其他元素.它不区分每个表行.它读取没有任何问题,我已经运行xml验证器,所以它不是语法问题.
public partial class lblXmlOutput : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Document;
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
XmlReader reader = XmlReader.Create(Server.MapPath("Part2XMLex.xml"), settings);
string result = "";
while (reader.Read())
{
if (reader.IsStartElement("td"))
result += reader.ReadElementContentAsString();
txtOutput.Text = result;
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试从我制作的字符串中读取一些Xml,但实际上任何Xml文件都可以.
我只是想浏览Xml节点,就像它是一个多维矩阵,并最终将它们放在DataTable中(将它们放在带有SqlBulkCopy的sql server中).我已经在MSDN和周围看了一下.有人能解释清楚简单吗?
这是代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Xml;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
private static DataTable table = new DataTable();
private static String xmlString =
@"<?xml version='1.0'?>
<!-- This is a sample XML document -->
<Garage>
<Car>
<Name>Ferrari</Name>
<Speed>360km/h</Speed>
<Engine>Ferrari Enzo</Engine>
<Color>Red</Color>
<Year>1999</Year>
</Car>
<Car>
<Name>Maserati</Name>
<Speed>270km/h</Speed>
<Color>Metal Grey</Color>
<Year>2007</Year>
</Car>
<Car>
<Name>Limo</Name>
<Color>Black</Color>
<Engine>Chevrolet</Engine>
<Year>2007</Year>
</Car>
</Garage>";
static void Main(string[] args)
{
Program x = new …Run Code Online (Sandbox Code Playgroud) 如何获取当前节点的XPath XMLReader?
例如:
<Employee>
<Entity>
<Id>1</Id>
</Entity>
</Employee>
Run Code Online (Sandbox Code Playgroud)
所以,我需要得到的XPath 1是Employee/Entity/Id.有任何想法吗?
using (var reader = XmlReader.Create(basePath, settings))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Text)
{
// need to get xpath of the text node
}
else if (reader.NodeType == XmlNodeType.Element)
{
// need to get xpath of the current node
}
}
}
Run Code Online (Sandbox Code Playgroud) xmlreader ×10
c# ×8
xml ×6
.net ×2
asp.net ×1
iphone ×1
nsdictionary ×1
objective-c ×1
xcode ×1
xml-parsing ×1
xmlnode ×1